[Supplement: Implementing servlet method] No need to configure servlet in .xml file

1. Premise

1. In the previous case, we have implemented servlet in three ways, but all of them need to configure the servlet in the .xml file. Now we will introduce a method that does not need to be configured in the .xml file.

2. In order to avoid conflicts, delete the servlet previously configured in the .xml file;

3. Create a class MyServletTest4 as before, and then inherit HttpServlet: extends HttpServlet;

At any position within the method {}, right-click Gernerate to override the method;

4. Modify the super statement in the method:

Change the super statement in the doGet method to: this.doPost(req, resp);

In the doPost method, write the output statement. It is just a test now to see if it can be used normally. Functional statements will be added later;

MyServletTest4.java:

package com.qingruan.servlet;

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(value="/myServlet4")
public class MyServletTest4 extends HttpServlet {

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

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("myServlet4...doPost");
    }
}

2. In the WEB-INF directory, create index.jsp and verify whether the connection is normal.

index3.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <base href="<%=basePath%>">
  <title>My JSP 'index.jsp' starting page</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
</head>
<body>
<%--myServlet1可改为我们在.xml文件中设置的myServlet2、myServlet3--%>
<form action="myServlet1" method="post">
  username:<input name="username"/><br/>
  password:<input name="password"/><br/>
  sex:<input  type="radio" name="sex" value="男" checked="true"/>男
  <input type="radio" name="sex" value="女"/>女<br/>
  hobby:<input type="checkbox" name="hobbys" value="song"/>song
  <input type="checkbox" name="hobbys" value="dance"/>dance
  <input type="checkbox" name="hobbys" value="draw"/>draw<br/>
  job:<select name="job">
  <option value="java开发工程师">java开发工程师</option>
  <option value="前端工程师">前端工程师</option>
  <option value="运维工程师">运维工程师</option>
</select>
  <input type="submit" value="提交"/>
</form>
</body>
</html>

3. Operation results

To run, select tomcat, select tomcat in the input box in the upper right corner of the idea page, and fix the project under Document, click OK;

After the operation is completed, the web page will automatically pop up with the following effect:

 Click the submit button and the page will jump. The user's account and password will be obtained later and compared with the user table information in the database. If the input is correct, you can jump to another page; if the account and password are wrong, you will not be able to jump. .

This is the application scenario of servlet.

Guess you like

Origin blog.csdn.net/xjj1128/article/details/127412586