[Web programming] experiment: request and response

1. Purpose of the experiment

(1) Master the transmission and acquisition of data when the browser requests.

(2) Master the transmission and acquisition of data when the server responds.

2. Experimental content

(1) Write a class that can display the index.jsp path in the browser address bar after accessing the Servlet;

(2) Please write a class that uses the getHeader("referer") method of the HttpServletRequest object to realize the function of anti-leeching of download resources.

3. Experimental requirements

(1) Master the HttpServletRequest object and its application

(2) Master the HttpServletResponse object and its application

(3) Master the implementation of request forwarding and request redirection

(4) Master how to solve the problem of Chinese garbled characters

(1) Do a good job of preview and clarify the purpose of the experiment

(2) Carefully record the test process and analyze the cause of the error

(3) Summarize the operation steps

4. Experimental steps and results (including program code and running screenshots)

(1) Write a class that can display the index.jsp path in the browser address bar after accessing the Servlet

Create an IndexServlet class, inherit the HttpServlet class, rewrite the doPost method inside, configure the url request path in web.xml, and use the sendRedirect method provided by the HttpServletResponse object to jump to the index.jsp page. Use the setCharacterEncoding method of HttpServletRequest and HttpServletRequest objects to process acquisition and encoding of corresponding data to avoid garbled characters.

Class code and screenshots:

package com.servlert;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class IndexServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // encoding req.setCharacterEncoding( "UTF-8" );         resp.setCharacterEncoding( "UTF-8" ); // redirection resp.sendRedirect( "index.jsp" );
        

        
        
            

}
}

 running result:

Type in the input field of the browser

http://localhost:8086/myservlet_war_exploded/indexServlet link, jump to the index.jsp page, the before and after operation effect diagram is as follows.

 After clicking the Enter key, enter the interface screenshot of the index.jsp page.

 (2) Please write a class that uses the getHeader("referer") method of the HttpServletRequest object to realize the function of anti-leeching of download resources.

Create a DownManagerServlet class, inherit the HttpServlet class, rewrite the doPost method inside, and configure the url request path in web.xml.

Class code and screenshots:

package com.servlert;

import javafx.print.Printer;

import javax.servlet.RequestDispatcher;
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 DownManagerServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // encoding req.setCharacterEncoding( "UTF-8" );         resp.setCharacterEncoding( "UTF-8" );         PrintWriter out=resp.getWriter() ; // Get the value of the referer header String referer = req.getHeader( "referer" ); // Get the access address String sitePart = "http://" +req.getServerName(); // Get whether the referer header is empty, this Whether the first address of the header starts with sitePart if (referer!=
        


        
        
        
        
        
        null &&referer.startsWith(sitePart)){
             // Process the downloading request out.print( "dealing download~" );        } else { // Illegal download request jumps to the down.html page RequestDispatcher rd = req.getRequestDispatcher( " /down.html" );            rd.forward(req,resp);        }    }}
            

            
            



 Code and screenshots of the page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" content="text/html;UTF-8">    <title>download</title>
</head>
<body>
    <a href="DownManagerServlet">
        download
    </a></body>
</html>

 Screenshot of running effect:

First visit:

 Second visit:

 5. Experimental reflection

(1) After creating the web project, create the IndexServlet class, inherit the HttpServlet, and rewrite the doPost or doGet methods inside, but the HttpServlet cannot be found. Through investigation and query, it is found that the servlet-api.jar architecture is not imported.

(2) Visit the url of the Servlet defined by yourself, but you can’t access it, and you find that you are not in web.xml

Configure the url path, and configure it in it.

(3) Add the href link to the a link in down.html. After clicking it, it is found that it is wrong and there is no response. I find that I have written the path of the url wrongly, and I have written an extra "/".

Guess you like

Origin blog.csdn.net/lf21qp/article/details/131370576