servler2

1. ServletContext interface

ServletContext represents the environment (context) object of a web application. The internal encapsulation of the ServletContext object is the information of the web application. There is only one ServletContext object per web application. ServletContext is created when the web project is loaded, and is unloaded or the server is closed destroyed when.

By instantiating a ServletContext implementation class object, we can set global variables or get configuration files.

2. Application

(1) Initialization parameter setting and acquisition

Instantiate the ServletContext object:

ServletContext servletContext = config.getServletContext();
ServletContext servletContext = this.getServletContext();

Set initialization parameters in XML:

<context-param>
      <param-name>JDBC</param-name>
      <param-value>com.mysql.jdbc.Driver</param-value>
</context-param>

Get parameters:

String name = context.getInitParameter("JDBC");

(2) Global attribute setting and acquisition

context.setAttribute("user", "admin");
String user = context.getAttribute("user");

The scope of the ServletContext domain object: the entire web application (all web resources can freely access data to the servletcontext domain, and the data can be shared)

(3) Obtain the absolute path of the configuration file in the web application

String path1 = context.getRealPath("123.txt");
String path2 = context.getRealPath("456.txt");
String path3 = context.getRealPath("789.txt");

Regardless of the folder in the web application, the file can be obtained and can be used to obtain the configuration file.

 3.response response

When the server receives a request, it will generate a response object, that is, response. We can implement different operations through the response.

(1) Response html code

copy code
public class LoginServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
        String user = request.getParameter("user");
        String pwd = request.getParameter("pwd");
        String result = null;
        if(user.equals("admin")&pwd.equals("123456"))
            result= "<h3 style='color:green'>登录成功</h3>";
        else
            result="<h3 style='color:red'>Incorrect account or password</h3>";
        response.getWriter().println(result);
    }
}
copy code

(2) Set the response header

copy code
public class ResponseServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.addHeader("Adam", "wink");
        response.addIntHeader("GOD", 123);
        response.addDateHeader("when", 19820052566222L);
        String html = "www.baidu.com";
        response.setStatus(302);
        response.setHeader("Location", html);//Realize page 302 jump
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
copy code
copy code
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("<h2>The page will jump to Baidu after 5s!</h2>");
        response.setHeader("refresh", "5;url=http://www.baidu.com");//Timed automatic jump
    }
copy code

(3) page jump:

Client jump:

Two requests, the first is to request the virtual path of the servlet, and the second is to request the html page

Two responses, the first time is 302, the page jumps, the second time is 200, the jump page loads

String html = "www.baidu.com";
response.setStatus(302);
response.setHeader("Location", html);//Realize page 302 jump

Server jump:

One request, the servlet is requested, and the page is opened inside the server

One response, 200, jump page opens

The server response will not change the current url

copy code
public class LoginServlet extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        String name = request.getParameter("name");
        String password = request.getParameter("password");
 
        if ("admin".equals(name) && "123".equals(password)) {
            request.getRequestDispatcher("success.html").forward(request, response);
        }
 
    }
 
}
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325515077&siteId=291194637