这周的JavaWeb作业正巧是运用MVC模式写一个网页
之前在软件工程理论课上老师提到了JavaWeb的作业,是让去了解了MVC模式,我一不小心写到软件工程理论的分类专栏里边了……以下是我作业的链接:
【软件工程理论与实践】Homework(五.1)——了解MVC与MVP的区别
现在我认为,MVC模式就是 用JavaBean构建模型(即构建能够实例化的类)、用JSP编写与用户交互的试图界面,用Servlet来编写控制器(即在Servlet中做判断,减少JSP页面的“<%%>”)。
以下是我的两个JSP界面:(登录界面、登陆成功提示界面)——View:
1、登录界面:
2、登陆成功界面:
红色下划线为存储的用户登录时的用户名
源代码:
一、两个JavaBean:
1、User类:
package Java;
public class User {
private String name;
private String pwd;
private String identify;
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;
}
public String getIdentify(){
return identify;
}
public void setIdentify(String identify){
this.identify=identify;
}
}
2、UserCheck类:
package Java;
public class UserCheck {
public boolean validateUserName(User user){
if(user.getName().equals("MVC"))
return true;
else return false;
}
public boolean validateUserPwd(User user){
if(user.getPwd().equals("password"))
return true;
else return false;
}
public boolean validateIdentity(User user){
if(user.getIdentify().equals("6759"))
return true;
else return false;
}
}
二、两个视图:
1、login.jsp
<%--
Created by IntelliJ IDEA.
User: Curry
Date: 2020/10/25
Time: 22:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>loginCheck.jsp</title>
</head>
<body>
<h2 style="margin-top: 150px"><p align="center"><font color="#6495ed">用户登录</font></p></h2>
<form action="LoginCheckServlet" method="post" >
<table align="center"">
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="UserName" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="UserPassword" /></td>
</tr>
<tr>
<td>图片验证码:</td>
<td><input type="text" name="UserIdentify" /></td>
</tr>
<tr>
<td colspan="2" align="center"><img src="image/IMG6759.jpg" style="width: 120px;length:100px;"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交" style="margin-right: 10px;" /><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
2、loginsuccess.jsp:
<%--
Created by IntelliJ IDEA.
User: Curry
Date: 2020/10/25
Time: 22:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<meta charset="UTF-8">
<title>loginSuccess</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="user" type="Java.User" scope="request"/>
Congratulations!<br>
Welcome,<jsp:getProperty name="user" property="name"/>!
</body>
</html>
三、一个Servlet: LoginCheckServlet:
package com.servlet;
import Java.User;
import Java.UserCheck;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "LoginCheckServlet",urlPatterns = "/LoginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
private static final long serialVersionUID=1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException {
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("UserName");
String pwd=request.getParameter("UserPassword");
String identity=request.getParameter("UserIdentify");
User user=new User();
user.setName(name);
user.setPwd(pwd);
user.setIdentify(identity);
UserCheck check=new UserCheck();
out.println("<html><body>");
if((check.validateUserName(user)==true)&&(check.validateUserPwd(user)==true&&(check.validateIdentity(user)==true))){
request.setAttribute("user",user);
RequestDispatcher dis=request.getRequestDispatcher("loginSuccess.jsp");
dis.forward(request,response);
}
else if((check.validateUserName(user)==false)||(check.validateUserPwd(user)==false)){
out.println("Please check your Username and your Password ");
response.setHeader("refresh","3;url=loginCheck.jsp");
}
else if(!(check.validateIdentity(user))){
out.println("The Verification code is wrong!Please retry.");
response.setHeader("refresh","3;url=loginCheck.jsp");
}
out.println("</body></html>");
}
}
运行界面: 登陆成功就是loginsucess.jsp界面,上面已放图,登录失败如下:
1、登录失败1(用户名/密码有误):
2、登录失败2(验证码有误):
提及我写这个代码时候的一个小错误
这个我第一次写成user.getName()==“MVC”,怎么样都不正确,后来才知道要用equals。。。。