第一步新建D web project
第二步新建servlet
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestPage
*/
@WebServlet("/TestPage")
public class TestPage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestPage() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
// PrintWriter out = response.getWriter();
// Date now = new Date(); // Date & Time
// String page = "<html><body><p>" + now +"</p></body></html>";
// out.println(page);
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html;charset=utf-8");
// 浏览器默认的编码是iso-8859-1转换成utf-8,如果不转换,会产生乱码。
String name = new String(username.getBytes("iso-8859-1"), "utf-8");
String pass = new String(password.getBytes("iso-8859-1"), "utf-8");
System.out.println("username" + name);
System.out.println("password" + pass);
//如果登陆名是张三,密码是123,登陆成功,否则登陆失败。
if ("abc".equals(name)&&pass.equals("123")) {
response.getWriter().write("登陆成功");
} else
response.getWriter().write("用户名或密码错误");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
新建web.xlm
<?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>TestPage</display-name>
<servlet>
<servlet-name>TestPage</servlet-name>
<servlet-class>TestPage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestPage</servlet-name>
<url-pattern>/TestPage</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
新建index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is my JSP page.
<br>
<form action="TestPage" method="get">
用户名:<input type="text" id="username" name="username" /> <br>
密码: <input type="text" id="password" name="password" /> <br> <input
type="submit" value="提交" />
</form>
</body>
</html>
运行