Three Methods of Getting Parameters in Java Web

Directory

Get default parameters from web.xml file

Obtain interface parameters from the URL interface of the browser

The method of inheriting the servlet interface obtains the interface parameters from the URL interface of the browser

Inherit the method of the HttpServlet interface to obtain interface parameters from the browser’s URL interface

Get form parameters from web form

Inherit the respective roles of doPost and doGet in Httpservlet


Get default parameters from web.xml file

First, you need to define and declare in the xml file, write the parameter name in parm-name under init-parm under servlet, parm-value Write the value of the parameter

<!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>
<!-- Create a servlet instance, similar to new in java-->
    <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>
<!-- Rules for configuring access -->
    <servlet-mapping>
        <servlet-name>MyServlet01</servlet-name>
        <url-pattern>/Lun.do</url-pattern>
<!-- .do is for jsp, use .do to access servlet-->
    </servlet-mapping>
</web-app>

In addition to the parameters declared in web.xml, the parameters must be received by variables in the servlet, and then sent to the browser for display.

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 {
        //Get the parameters in the configuration file, use an enumeration structure to receive
        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 {
        //Every time a request is made, the service() method will be called once to set the character set
        servletResponse.setContentType("text/html;charset=UTF-8");
        //Use the PrintWriter.write() method to output content to the foreground page
        PrintWriter writer = servletResponse. getWriter();
        writer.write("Learn servlet, start a new journey <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() {
    }
}

Get interface parameters from the browser’s URL interface

The method of inheriting the servlet interface obtains the interface parameters from the browser’s URL interface

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 {
// The method of inheriting servlet is not very common

    private String IU_name;
    private String IU_password;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //Get the parameters in the configuration file, use an enumeration structure to receive
        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 {

        //downcast
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        //Set request encoding
        httpServletRequest.setCharacterEncoding("utf-8");

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

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

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

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

        //Set the response character set encoding
        servletResponse.setContentType("text/html;charset=UTF-8");
        //Use the PrintWriter.write() method to output content to the foreground page
        PrintWriter writer = servletResponse. getWriter();
        writer.write("Learn servlet, start a new journey <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() {
    }
}

The method of inheriting the HttpServlet interface obtains the interface parameters from the browser’s URL interface

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;

//This is similar to the object and value of the access path and parameters configured in the web.xml file
@WebServlet(value = {"/ms4.do", "/ms5.do"},
        initParams = {@WebInitParam(name = "name", value = "default name (Zhao San)"),
                @WebInitParam(name = "password", value = "default password (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())
        {
            //If there is still an element, take out the element
            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;
        //Set the encoded character set of the response object
        resp.setContentType("text/html;charset=utf-8");
        //Set the encoded character set of the request object
        resp.setCharacterEncoding("UTF-8");
        // output information
        PrintWriter writer = resp. getWriter();
        String um = req. getParameter("name");
        String hsr = req. getParameter("password");
        if ( um != null & amp; & amp; !"".equals(um) & amp; & amp; !"null".equals(um) )
            name = um;
        else
            name = IU_name;
        if ( hsr != null & amp; & amp; !"".equals(hsr) & amp; & amp; !"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 {
        //Set request encoding
        req.setCharacterEncoding("utf-8");
        this. doGet(req, resp);
    }
}

Get form parameters from web form

Inherit the respective functions of doPost and doGet in Httpservlet

doPost is to receive a request, and the request is sent by the

form tag.

doGet is to receive the response and respond to the browser, so when the browser enters the URL, it will call diGet and then respond to the browser.

<%-- method can choose to post or get, but we generally post requests --%>
<form action="/uned_war/ms3.do" method="post">
        User: <input type="text" name="username" />
        Password: <input type="password" name="password" />
        <input type="submit" value="submit">
</form>

Write the form in the jsp file, and then input parameters in the form of the browser

Receive parameters in the servlet and then respond to the browser

<%@page language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>ti mu</title>
</head>
<body>
    <form action="/unwar/ms3.do" method="post">
        User: <input type="text" name="username" />
        Password: <input type="password" name="password" />
        <input type="submit" value="submit">
    </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 {
        // Get the content of the interface
        String username = req. getParameter("username");
        String password = req. getParameter("password");
        //Set the encoded character set of the response object
        resp.setContentType("text/html;charset=utf-8");
        // output information
        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 {
        //Set request encoding
        req.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
}