实验要求
参考教材P142的例子,使用JSP、Servlet、JavaBean实现基于MVC模式的用户登录验证程序
按照课本敲的代码
文件的位置是这样的
package dto;
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd=pwd;
}
}
package service;
import dto.User;
public class UserCheck {
public boolean validate(User user) {
if(user!=null&&user.getName().equals("JSPMVC")) {
if(user.getPwd().equals("MVC")) {
return true;
}
return false;
}
return false;
}
}
package servlet;
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;
import service.UserCheck;
import dto.User;
/**
* Servlet implementation class demo1
*/
@WebServlet("/LoginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginCheckServlet() {
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());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
User user=new User();
user.setName(name);
user.setPwd(pwd);
UserCheck uc=new UserCheck();
if(uc.validate(user)) {
request.setAttribute("user", user);
RequestDispatcher dis=request.getRequestDispatcher("loginSuccess.jsp");
dis.forward(request,response);
}else {
response.sendRedirect("loginCheck.jsp");
}
}
}
logincheck.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginCheckServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="pwd"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
loginsuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="user" class="dto.User" scope="request"/>恭喜
<jsp:getProperty property="name" name="user"/>登录成功!
</body>
</html>
运行结果