1.创建项目:
动态web项目—创建MyJsp.jsp的JSP文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="./MyServlet" method="post">
<p>账号<input type="text" name="id"></p>
<p>密码<input type="text" name="password"></p>
<p><input type="submit" name="登陆"></p>
</form>
</body>
</html>
注:action里./MyServlet很重要,映射servlet。
2.新建控制器—Servlet
src目录下新建一个包,包名为cc.MyMVC.servlet。然后,再在该包下新建一个servlet文件,名为MyServlet:
package cc.MyMVC.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 cc.MyMVC.POJO.MyPOJO;
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
public MyServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//以下为关键代码
public void doPost(HttpServletRequest request, HttpServletResponse response)//处理post请求的方法
throws ServletException, IOException {
boolean b=false;
MyPOJO myPOJO=new MyPOJO();//新建MyPOJO类的对象myPOJO
//根据标示名获取JSP文件中表单所包含的参数
String id=request.getParameter("id");
String password=request.getParameter("password");
b=myPOJO.login(id,password);//使用模型对账号和密码进行验证,返回一个boolean类型的对象
if(b){ //如果验证结果为真,跳转至登录成功页面
response.sendRedirect("LoginSuccessful.jsp");
}
else { //如果验证结果为假,跳转至登录失败页面
response.sendRedirect("LoginFailed.jsp");
}
}
public void init() throws ServletException {
}
}
3.新建Model的POJO文件
POJO(Plain Ordinary Java Objects)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。POJO实质上可以理解为简单的实体类,顾名思义POJO类的作用是方便程序员使用数据库中的数据表。
在src文件下新建包,包名为cc.MyMVC.POJO。然后在该包下新建class文件,名为MyPOJO。
package cc.MyMVC.POJO;
public class MyPOJO {
public boolean login(String id,String password){
if(id.equals("admin")&&password.equals("123456")){//判断用户名以及密码是否与设定相符
return true;
}
else return false;
}
}
注:@WebServlet("/MyServlet")这句话很重要
4.在Web-Content文件夹下新建两个jsp文件,分别命名为LoginSuccessful.jsp以及LoginFailed.jsp。
LoginSuccessful.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
Login Successful. <br>
</body>
</html>
LoginFailed.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
Login Failed. <br>
</body>
</html>
5.运行看执行效果
运行myjsp.jsp文件,登录名:admin,密码:123456,看能否跳转到LoginSuccessful.jsp界面。