1.三要素
(1) 入口 就是我们所在的页面
入口到处理的数据请求会出现乱码,用request.SetCharacterEncoding("UTF-8");来解决,仅仅是用用于Post请求。
(2)处理 就是在Servlet中执行的内容
处理到出口之间也会出现乱码,用response.SetCharacterEncoding("UTF-8")来解决
(3)出口 就是我们跳转的页面
web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.beiwo.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.首先写一个登录界面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="LoginServlet" method="get">
账号:<input type=text name="username"><Br>
密码:<input type=text name="pwd"><Br>
<input type="submit"?username="aaa"&pwd="123" value="登录">
</form>
//当我们将鼠标放置登录按钮上面时会出现用户名跟密码,不安全
<a href="Success.jsp?username=aaa&pwd=123" >登录</a>
</body>
</html>
3.点击登录之后会进入servlet
package com.beiwo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("进入了doGet方法");
String name = request.getParameter("username");
String password = request.getParameter("pwd");
System.out.println("用户名:" + name + "密码:" + password);
request.setAttribute("username", name);
request.setAttribute("pwd", password);
//跳转并且传递参数
request.getRequestDispatcher("Success.jsp").forward(request, response);
//html文件不能够传递参数,只有jsp文件可以传递参数
//跳转不带参数
response.sendRedirect("Success.jsp");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("进入了doPost方法");
//拿到用户名和密码
String name = request.getParameter("username");
String password = request.getParameter("pwd");
System.out.println("用户名:" + name + "密码:" + password);
//保存用户名和密码
request.setAttribute("username", name);
request.setAttribute("pwd", password);
//跳转并且传递参数
request.getRequestDispatcher("Success.jsp").forward(request, response);
//跳转不带参数
//response.sendRedirect("Success.jsp");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
4.跳转到另外一个页面,并且拿到 用户名跟密码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
//在这里首先要将编码格式改为utf-8,否则会出现乱码 <%@ page pageEncoding="UTF-8"%>
<html> <head> <title>Success.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> //当我们拿数据的时候要用${} 用户名:${username} 密码:${pwd} </body> </html>