[Web programming] experiment: Servlet basic application

1. Purpose of the experiment

(1) Master the basic and core knowledge of java web applications: servlet.

(2) Understand the specific use of servlets.

2. Experimental content

(1) Write a servlet to realize the function of counting the number of visits to the website;

(2) Please write a program so that the program can read the configuration information of the servlet, obtain the parameter value corresponding to the parameter name encoding, and output it to the page.

3. Experimental requirements

(1) Master the use of Servlet interface and its implementation class

(2) Understand the life cycle of Servlet

(3) Proficiency in using idea tools to develop Servlets

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

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

(6) Summarize the operation steps

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

package com.web.www;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CountServlet",value="/css")
public class CountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext=request.getServletContext();
        Integer count =(Integer)servletContext.getAttribute("count");
        if(count==null) {
            count = 1;
            servletContext.setAttribute("count",count);
        }
        else{
            count++;
            servletContext.setAttribute("count",count);
        }
        response.getWriter().println(count);
    }

 

 

5. Experimental reflection

1. The method I used at the beginning was probably:

protectedvoid doGet(HttpServletRequest request,HttpservletResponse)throws ServletException;

num++;

System.out.printnln("You are the "+count+"th visit page")

But he can only count once

2. When inheriting the httpsservlet at the beginning, there was always a wavy line below it. Later, I checked it online because there was a problem with the servlet.jar package.

Guess you like

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