实验3 Servlet基础

实验要求
参考教材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>

运行结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值