目录

从web.xml文件中获取默认参数

从浏览器的网址界面获取界面参数

继承servlet接口的方法从浏览器的网址界面获取界面参数

继承HttpServlet接口的方法从浏览器的网址界面获取界面参数

从网页表单中获取表单参数

继承Httpservlet中的doPost和doGet各自的作用


从web.xml文件中获取默认参数

首先需要在xml文件中定义和声明,在servlet下的init-parm下的parm-name写入参数名字,parm-value写入参数的值

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
<!--    创建一个servlet实例,类似java中的new-->
    <servlet>
        <servlet-name>MyServlet01</servlet-name>
        <servlet-class>com.zpark.servlet.MyServlet01</servlet-class>

        <init-param>
            <param-name>name</param-name>
            <param-value>Lungcen</param-value>
        </init-param>
        <init-param>
            <param-name>password</param-name>
            <param-value>110120130</param-value>
        </init-param>
    </servlet>
<!--    配置访问的规则-->
    <servlet-mapping>
        <servlet-name>MyServlet01</servlet-name>
        <url-pattern>/Lun.do</url-pattern>
<!--        .do是针对jsp来的,以.do来访问servlet-->
    </servlet-mapping>
</web-app>

除了web.xml内声明了参数,还要在servlet中利用变量接收参数,再发送到浏览器中显示出来。

package com.zpark.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

public class MyServlet01 implements Servlet {
    private String name;
    private String password;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获得配置文件中的参数,用一个枚举结构来接收
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while(initParameterNames.hasMoreElements())
        {
            String tump = initParameterNames.nextElement();
            if ("name".equals(tump))
                name = servletConfig.getInitParameter(tump);
            if ("password".equals(tump))
                password = servletConfig.getInitParameter(tump);
        }
    }
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse)
            throws ServletException, IOException {
        //每次请求,都会调用一次 service() 方法,设置字符集
        servletResponse.setContentType("text/html;charset=UTF-8");
        //使用PrintWriter.write()方法向前台页面输出内容
        PrintWriter writer = servletResponse.getWriter();
        writer.write("学习servlet,开启新征程 <br />");
        writer.write("name ->" + name + "<br />");
        writer.write("password ->" + password);
        writer.close();
    }
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
    @Override
    public String getServletInfo() {
        return null;
    }
    @Override
    public void destroy() {
    }
}

filter java 获取body参数 java获取-d参数_表单

从浏览器的网址界面获取界面参数

继承servlet接口的方法从浏览器的网址界面获取界面参数

package com.zpark.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

public class MyServlet01 implements Servlet {
//    继承servlet的方法不是很常用

    private String IU_name;
    private String IU_password;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获得配置文件中的参数,用一个枚举结构来接收
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while(initParameterNames.hasMoreElements())
        {
            String tump = initParameterNames.nextElement();
            if ("name".equals(tump))
                IU_name = servletConfig.getInitParameter(tump);
            if ("password".equals(tump))
                IU_password = servletConfig.getInitParameter(tump);
        }
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse)
            throws ServletException, IOException {

        //向下转型
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        //设置请求编码
        httpServletRequest.setCharacterEncoding("utf-8");

        String name;
        String password;
        String name_hsr = httpServletRequest.getParameter("name");

        if(name_hsr != null && !"".equals(name_hsr) && !"null".equals(name_hsr) )
            name = httpServletRequest.getParameter("name");
        else
            name = IU_name;

        String password_hsr = httpServletRequest.getParameter("password");

        if(password_hsr != null && !"".equals(password_hsr) && !"null".equals(password_hsr) )
            password = httpServletRequest.getParameter("password");
        else
            password = IU_password;

        //设置下响应字符集编码
        servletResponse.setContentType("text/html;charset=UTF-8");
        //使用PrintWriter.write()方法向前台页面输出内容
        PrintWriter writer = servletResponse.getWriter();
        writer.write("学习servlet,开启新征程 <br />");
        writer.write("name ->" + name + "<br />");
        writer.write("password ->" + password);
        writer.close();
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
    }
}

继承HttpServlet接口的方法从浏览器的网址界面获取界面参数

package com.zpark.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

//这个类似于再web.xml文件的配置的访问路径和参数的对象和值
@WebServlet(value = {"/ms4.do", "/ms5.do"},
        initParams = {@WebInitParam(name = "name", value = "默认姓名(赵三)"),
                @WebInitParam(name = "password", value = "默认密码(110120130)")}
)
public class MyServlet4 extends HttpServlet {
    private String IU_name = "";
    private String IU_password = "";
    @Override
    public void init(ServletConfig config) throws ServletException {
        Enumeration<String> ParameterNames = config.getInitParameterNames();
        while (ParameterNames.hasMoreElements())
        {
            //如果还存在元素,就拿出元素
            String temp = ParameterNames.nextElement();
            if ("name".equals(temp))
                IU_name = config.getInitParameter(temp);
            if ("password".equals(temp))
                IU_password = config.getInitParameter(temp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name;
        String password;
        //设置响应对象的编码字符集
        resp.setContentType("text/html;charset=utf-8");
        //设置请求对象的编码字符集
        resp.setCharacterEncoding("UTF-8");
        //输出信息
        PrintWriter writer = resp.getWriter();
        String um = req.getParameter("name");
        String hsr = req.getParameter("password");
        if ( um != null && !"".equals(um) && !"null".equals(um) )
            name = um;
        else
            name = IU_name;
        if ( hsr != null && !"".equals(hsr) && !"null".equals(hsr) )
            password = hsr;
        else
            password = IU_password;
        writer.write("username:" + name);
        writer.write("<br/>");
        writer.write("password:" + password);
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("utf-8");
        this.doGet(req, resp);
    }
}

filter java 获取body参数 java获取-d参数_表单_02

filter java 获取body参数 java获取-d参数_表单_03

从网页表单中获取表单参数

继承Httpservlet中的doPost和doGet各自的作用

doPost是接收请求,请求是由<form>表单标签来发送请求。

doGet是接收响应,响应给浏览器,所以再浏览器网址回车的时候,会调用diGet然后响应给浏览器。

<%--    method可以选择是post还是get,但是我们一般是post请求--%>
<form action="/uned_war/ms3.do" method="post">
        用户:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="提交">
</form>

在jsp文件中写入表单,然后再浏览器的表单输入参数

在servlet中接收参数然后再响应到浏览器

<%@page language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>ti mu</title>
</head>
<body>
    <form action="/unwar/ms3.do" method="post">
        用户:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="提交">
    </form>
</body>
</html>
package com.zpark.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyServlet3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //获取界面的内容
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //设置响应对象的编码字符集
        resp.setContentType("text/html;charset=utf-8");
        //输出信息
        PrintWriter writer = resp.getWriter();
        writer.write("Hello world" + "<br/>");
        writer.write("username ->" + username + "<br/>");
        writer.write("password ->" + password + "<br/>");
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
}

filter java 获取body参数 java获取-d参数_servlet_04