一、实验目的:
1. 掌握JSP语法;
2. 了解JSP生命周期;
3. 掌握JSP请求转发和包含。
二、实验内容:
1. 创建login.jsp页面,其有用户名,密码及对应的文本框;
2. 创建ControlLogin.java类,验证用户名和密码是否正确,如果用户名等于自己的学号,密码等于123456,则跳转到index.jsp页面,并显示欢迎用户名您好,否则显示登录失败。
三、软硬件环境:
jdk1.8,tomcat9,eclipse for Java EE。
四、实验过程:
1.打开eclipse,File—new—Daynamic Web Project—(Project name:)txt—(Target runtime)选中Tomcat9.0—安装路径—JRE(jdk1.8)—Finish;
2.新建一个login.jsp文件。找到WebContent右键—new—(或者other—Web—)JSPFile—Next—FileName:login.jsp—Finish.
login.jsp文件代码
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!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=GB2312">
<title>Insert title here</title>
</head>
<body>
<form action = "clogin" method = "post">
<table>
<tr>
<td> 用户名; </td>
<td> <input type = "text" name = "username"> </td>
</tr>
<tr>
<td> 密码; </td>
<td> <input type = "password" name = "pwd"> </td>
</tr>
<tr>
<td> <input type = "submit" value = "提交">
<input type = "reset" value = "重置">
</td>
</tr>
</table>
</form>
</body>
</html>
3.导Servlet包。总项目txt—右键,选Properties—Java Build Path—Libraries—最右边选第二个Add External—Tomcat9—lib—Servlet-api.jar—打开—apply and close。
4.Java Resources---src--右键--创建Servlet文件——Java package: mypackage—class name:Controllogin--Next--URL mappings:"/ControlLogin"-->"/clogin"--ok---勾起doPost, doGet--next--Finish
Controllogin.java代码
package mypack;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CLogin")
public class Controllogin extends HttpServlet {
private static final long serialVersionUID = 1L;
public Controllogin() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("username");
String pwd = request.getParameter("pwd");
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
RequestDispatcher dispatcher1 = request.getRequestDispatcher("/Error.jsp");
if (user.equals("tom") && pwd.equals("tom"))
{
System.out.println("成功");
dispatcher.forward(request, response);
}
else
{
System.out.println("密码错误2");
dispatcher1.forward(request, response);
}
}
}
5.创建一个index.jsp文件(步骤同2)
index.jsp代码
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!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=GB2312">
<title>Insert title here</title>
</head>
<body>
<%
String user=request.getParameter("username");
out.print("欢迎"+user+"登录");
%>
</body>
</html>
6.创建Error.jsp文件(步骤同2)
Error.jsp代码
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!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=GB2312">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<br> <br> <br> 登录失败请重新登录
</div>
<form action="ControlLogin">
<table align="center">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码::</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td><input type="submit" value="提交"> <input
type="reset" value="重置"></td>
</tr>
</table>
</body>
</html>
7.实验结果:
login.jsp文件——右键——Run as——第一个选项——Finish
index.jsp文件-----Run as
Error.jsp----Run as
细节问题:1.把ISO-8859-1统一换成GB2312
如有不妥之处还望大家指出,相互交流学习哈!