这篇文章总字数为:4319 字,有 0 张图存于本站服务器
实验总结:
在login.java中有个判断语句,它通把login.html前端表单提交过来的账号密码与在web.xml中提取的账号和密码作比较,如果账号密码都相同则通过审核,执行请求转发跳转到welcome.jsp显示welcome,用户名(其中用户名动态传参显示),若不相同则执行重定向跳转到error.jsp显示登陆失败点击重新登录回到login.html
关键知识:
- 请求转发:request.getRequestDispatcher("/welcome.jsp").forward(request, response);
- 重定向:response.sendRedirect("/ping17180074/error.jsp");
- 请求转发跳转规则地址栏url不变
- 重定向跳转地址url改变
效果预览:
1.输入错误账号密码时
2.输入正确账号密码时
视频步骤:(3分钟)
文章底部可下载完整代码带现成实验模板
实验代码:
代码中学号姓名记得删改!!!
login.html文件(位置:根目录/WebRoot/)
<!DOCTYPE html>
<html>
<body>
<form action="servlet/login" method="post">
输入账号:<input type="text" name="user">
<br>
<br>
输入密码:<input type="password" name="pwd">
<br>
<br>
<input type="submit" value="登录">
</form>
</body>
</html>
login.java文件(位置:根目录/src/servlet/)
package 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 login extends HttpServlet {
/**17180074平帅
*
*/
private static final long serialVersionUID = 618236416112505770L;
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
//获得初始化参数
String ouserName=this.getInitParameter("ouserName");
String ouserPwd=this.getInitParameter("ouserPwd");
//从request接收表单数据
request.setCharacterEncoding("GBK");
String name=request.getParameter("user");
String pass=request.getParameter("pwd");
//通过response向客户端应答,显示接收到的数据
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>处理表单</TITLE></HEAD>");
out.println("<BODY>");
if(name.equals(ouserName)&&pass.equals(ouserPwd)){
request.getRequestDispatcher("/welcome.jsp").forward(request, response);//请求转发跳转
}else{
response.sendRedirect("/ping17180074/error.jsp");//重定向跳转
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
welcome.jsp文件(位置:根目录/WebRoot/)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("user");
%>
welcome,<%=name %><br>
</body>
</html>
error.jsp文件(位置:根目录/WebRoot/)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>errorpage</title>
</head>
<body>
登录失败,请重新<a href="<%=request.getContextPath()%>/login.html">登录</a>
</body>
</html>
web.xml文件(位置:根目录/WebRoot/WEB-INF/)
<?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>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>register</servlet-name>
<servlet-class>servlet.register</servlet-class>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>servlet.login</servlet-class>
<init-param>
<param-name>ouserName</param-name>
<param-value>17180074</param-value>
</init-param>
<init-param>
<param-name>ouserPwd</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/servlet/register</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
评论