current position:Home>Summary of JavaWeb knowledge points 1: servlet code example 1: helloservlet

Summary of JavaWeb knowledge points 1: servlet code example 1: helloservlet

2022-01-27 02:56:24 ^Brooke's heartbeat^

Summary of knowledge points 1:Servlet

  1. get/post

get: Requests can carry fewer parameters , There's a limit to the size , Will be in the browser URL The address bar displays the data content , unsafe , But efficient .

post: There is no limit to the number of parameters a request can carry , There is no limit to size , Not in the browser URL The address bar displays the data content , Security , But it's not efficient .

  1. Response code

200: Request response successful

3xx: request redirections ( You go back to the new position I gave you ) 302

4xx: Resource not found ( Resource does not exist ) 404

5xx: Server code error 500 Gateway error 502

  1. Request forwarding / request redirections

One web After the resource receives a client request , Tell the server to call another web Resources to deal with , Call it request forwarding /307.

One web After the resource receives a client request , Tell the browser to visit another web Resources to deal with , Call it request redirection /302.

  1. web.xml Set up the welcome interface

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

Code example 1:HelloServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// Set the response content type to text/html( Text / Hypertext markup language )

response.setContentType("text/html");

// tell servlet use UTF-8 transcoding , Instead of using the default ISO8859

response.setCharacterEncoding("UTF-8");

// 1, from HttpServletResponse in get One PrintWriter; ,

// 2, A popular analogy is through HttpServletResponse Object gets a pen , And then you can use it out.print() Method to write anything you want to display on the web page .

//    out.print("<html><body>");

//    out.print(" Code block "");

//    out.print("</body></html>");

// 3, adopt PrintWrite, Output in stream mode html, Return to the client , Displayed in the IE On .

// 4, Take a stream object that responds to the client

// 5, obtain PrintWriter flow , Used to output... On the client .

// Get the output stream of the response

PrintWriter out = response.getWriter();

        out.println("<html>");

        out.println("<head>");

        out.println("<title>HelloServlet</title>");

        out.println("</head>");

        out.println("<body>");

        out.println("<h1>HelloServlet !</h1>");

        out.println("<h2> Hello Servlet !</h2>");

        out.println("</body>");

        out.println("</html>");

}

 

copyright notice
author[^Brooke's heartbeat^],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270256201470.html

Random recommended