接着下面的文章和项目,我们继续往下走(不熟悉的可以看看前面的文章找找里面建的HelloJSP项目),看看写一个javabean,然后看看它的作用是什么。
一、添加一个Java类
在项目中添加一个名为UserBean的类,如下图所示:
代码修改如下:
package com.bean;
public class UserBean {
private String username;
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/*
* 校验用户输入数据
* @return
* */
public boolean validate()
{
if(username.equals("") || password.equals(""))
return false;
return true;
}
/*
* 模拟数据库校验
*
* */
public boolean login()
{
if(username.equals("123456") && password.equals("123456"))
return true;
return false;
}
@Override
public String toString()
{
return "UserBean [username=" + username +",password="+password+"]";
}
}
二、修改HelloWorld.jsp文件
既然前面创建了一个javabean,那我们接下来就看看怎么用它,修改我们早前创建的helloworld.jsp文件如下。
<%@page import="com.bean.UserBean" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login demo</title>
</head>
<body>
<div style="display: flex">
<form name="myform" method="post" action="TestServlet" style="margin: auto">
UserName<input name="username" type="text" />${user.username }
<br>
PassWord<input name="userpw" type="password" />
<br>
<div>
<input name="Submit" type="submit" value="Submit">
<input name="Reset" type="reset" value="Reset"><br>
</div>
</form>
</div>
</body>
</html>
三、修改TestServlet.java
修改doPost表单响应如下:
package com.servlet;
import java.io.IOException;
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 com.bean.UserBean;
/**
* Servlet implementation class TestServlet
*/
@WebServlet(description = "This is my first Servlet", urlPatterns = { "/TestServlet" })
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public TestServlet() {
// 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
//doGet(request, response);
toLogin(request,response);
}
private void toLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
{
//获取请求参数
String username=req.getParameter("username");
String password=req.getParameter("userpw");
//验证并封装参数
UserBean user = new UserBean();
user.setUsername(username);
user.setPassword(password);
//调用业务对象和方法
if(user.login())
{
//resp.sendRedirect("LoginSucess.jsp");
req.setAttribute("user", user);
req.getRequestDispatcher("LoginSucess.jsp").forward(req, resp);
}
else
{
req.setAttribute("user", user);
req.getRequestDispatcher("HelloWorld.jsp").forward(req, resp);
}
}
}
这里就是简单处理一下响应,成功转到LoginSucess.jsp文件(页面显示字符串javabean+username+password),失败转到原来的登录页面(后面显示上次输入用户名)。
四、修改一下LoginSucess.jsp
<%@page import="com.bean.UserBean" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Loin Successful</h1>
<h1>
${user.toString() }
</h1>
</body>
</html>
五、运行结果
输入username=123456 和password=123456,成功登录,显示如下:
当输入的不是上面的组合后,登录失败,返回到helloworld页面,并在用户名后显示填写的用户名,显示如下:
六、总结
本文采用了Servlet+JSP+JavaBean(Web MVC)架构,MVC=MODEL VIEW CONTROLLER,其中的UserBean为Model,TestServlet为Controller,而JSP为View视图,在UserBean中的业务逻辑处理toLogin。此处为简单的MVC应用,后续针对MVC再有文章展开,此处大家理解就好,在那个层次做哪些事情有基本了解就行。
参考资料:
https://blog.csdn.net/sinat_41655101/article/details/79536771