1、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h4>用户登录</h4>
<form action="http://localhost:8080/DemoServlet/myServlet2" method="get">
姓名:<input type="text" name="userName" align="right"><br>
密码:<input type="password" name="passWord" align="right"><br>
<span style="">
<input type="submit" value="登录">
<input type="submit" value="忘记密码">
</span>
</form>
</div>
</body>
</html>
2、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>DemoServlet</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>myServlet2</servlet-name>
<servlet-class>com.demo.servlet.MyServlet2</servlet-class>
<init-param>
<param-name>myServlet1</param-name>
<param-value>myServletValue1</param-value>
</init-param>
<init-param>
<param-name>myServlet2</param-name>
<param-value>myServletValue2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myServlet2</servlet-name>
<url-pattern>/myServlet2</url-pattern>
</servlet-mapping>
</web-app>
3、继承HttpServlet
package com.demo.servlet;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet2 extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
String characterEncoding = req.getCharacterEncoding();
System.out.println("***CharacterEncoding: "+characterEncoding);
String contentType = req.getContentType();
System.out.println("***ContentType: "+contentType);
int contentLength = req.getContentLength();
System.out.println("***ContentLength: "+contentLength);
String localAddr = req.getLocalAddr();
System.out.println("***LocalAddr: "+localAddr);
Locale locale = req.getLocale();
System.out.println("***Locale: "+locale);
String localName = req.getLocalName();
System.out.println("***LocalName: "+localName);
int localPort = req.getLocalPort();
System.out.println("***LocalPort: "+localPort);
ServletContext servletContext = req.getServletContext();
System.out.println("***servletContext: "+servletContext);
String contextPath = req.getContextPath();
System.out.println("***ContextPath: "+contextPath);
String protocol = req.getProtocol();
System.out.println("***Protocol: "+protocol);
String method = req.getMethod();
System.out.println("***Method: "+method);
StringBuffer requestURL = req.getRequestURL();
System.out.println("***requestURL: "+requestURL);
String requestURI = req.getRequestURI();
System.out.println("***requestURI: "+requestURI);
String queryString = req.getQueryString();
System.out.println("***QueryString: "+queryString);
String serverName = req.getServerName();
System.out.println("***ServerName: "+serverName);
String scheme = req.getScheme();
System.out.println("***Scheme: "+scheme);
int serverPort = req.getServerPort();
System.out.println("***ServerPort: "+serverPort);
String servletPath = req.getServletPath();
System.out.println("***ServletPath: "+servletPath);
String userName = req.getParameter("userName");
System.out.println("***userName: "+userName);
String password = req.getParameter("passWord");
System.out.println("***password: "+password);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
}
}
4、页面请求
5、后台接收请求打印
***CharacterEncoding: null
***ContentType: null
***ContentLength: -1
***LocalAddr: 0:0:0:0:0:0:0:1
***Locale: zh_CN
***LocalName: 0:0:0:0:0:0:0:1
***LocalPort: 8080
***servletContext: org.apache.catalina.core.ApplicationContextFacade@4867dbd4
***ContextPath: /DemoServlet
***Protocol: HTTP/1.1
***Method: GET
***requestURL: http://localhost:8080/DemoServlet/myServlet2
***requestURI: /DemoServlet/myServlet2
***QueryString: userName=admin&passWord=admin
***ServerName: localhost
***Scheme: http
***ServerPort: 8080
***ServletPath: /myServlet2
***userName: admin
***password: admin