1.web.xml中的内容(myeclipse在创建web项目上要点击下一步勾上)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<description>配置用户登录的servlet</description>
<display-name>loginservlet</display-name>
<servlet-name>loginservlet</servlet-name>
<servlet-class>servlet.loginservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginservlet</servlet-name>
<url-pattern>/loginservlet</url-pattern>
</servlet-mapping>
</web-app>
2.初始登陆界面
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
<form method="post" action="loginservlet">
用户姓名:<input type="text" name="name"><br>
用户密码:<input type="password" name="pwd"><br>
<input type="submit" name="submit" value="确定">
</form>
</body>
</html>
3.以teacher身份登陆成功后显示的页面
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Teacher Page</title>
</head>
<body>
<h1>教师页面!</h1>
</body>
</html>
4.以student身份登陆后显示的页面
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Page</title>
</head>
<body>
<h1>学生页面!</h1>
</body>
</html>
5.出错界面
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<body>
<h1>输入有误!</h1>
</body>
</html>
6.控制器
package servlet;
import bean.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
@SuppressWarnings("serial")
public class loginservlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String username=request.getParameter("name");
String password=request.getParameter("pwd");
userbean user=new userbean();
user.setUsername(username);
user.setPwd(password);
if(user.login()){
request.setAttribute("user",user);
if(user.username.equals(user.user1)&& user.pwd.equals(user.password1))
getServletConfig().getServletContext().getRequestDispatcher("/teacher.jsp").
forward(request,response);
if (user.username.equals(user.user2)&& user.pwd.equals(user.password2))
getServletConfig().getServletContext().getRequestDispatcher("/student.jsp").
forward(request,response);
}else{
getServletConfig().getServletContext().getRequestDispatcher("/error.jsp").
forward(request,response);
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
}
7.JavaBean模型
package bean;
public class userbean {
public String user1="teacher";
public String password1="teacher";
public String user2="student";
public String password2="student";
public String username="";
public String pwd="";
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return this.username;
}
public void setPwd(String password) {
this.pwd = password;
}
public String getPwd() {
return this.pwd;
}
public boolean login() {
boolean temp = false;
if(username.equals(user1) && pwd.equals(password1)) {
temp=true;
}else if(username.equals(user2) && pwd.equals(password2)) {
temp=true;
}else {
temp=false;
}
return temp;
}
}
运行结果