运用MVC实现网站的登录功能。

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;
	}
	}

运行结果

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值