MVC循序渐进(jsp model2实现的登录)

类别:Java 点击:0 评论:0 推荐:

接上篇 MVC循序渐进(jsp model1实现的登录)
1、用jsp  model1实现用户登录

(1)、eclipse菜单 File->New->Folder     文件夹名称   model2

(2)、新建jsp文件

     webapp\WEB-INF\src\model2\LoginServlet.java    (相对于eclipse目录结构)
         webapp\model2\ login.jsp
         webapp\model3\ succeed.jsp 
         webapp\WEB-INF\web.xml
(3)、
LoginServlet.java
************************************************************
package model2;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws IOException, ServletException {
  process(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws IOException, ServletException {
  process(request, response);
 }

 public void process(HttpServletRequest request, HttpServletResponse response)
   throws IOException {
  response.setContentType("text/html");
  response.setCharacterEncoding("GBK");
  PrintWriter out = response.getWriter();
  boolean isSucceed = false;
  String msg = "";
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  if (username != null) {
   System.out.println("登陆处理开始.........");
   username = username.trim();
   password = password.trim();
   if (username.equals("") && username.equals("")) {
    isSucceed = false;
    msg = "用户名或密码为空";
   } else {
    isSucceed = true;
   }
   if (isSucceed) {
    if (username.equals("zkjbeyond")
      && password.equals("zkjbeyond")) {
     isSucceed = true;
     HttpSession session = request.getSession();
     msg = "登陆成功";
     session.setAttribute("username", username);

    } else {
     isSucceed = false;
     msg = "用户名密码不正确";
    }
   }
  } else {
   isSucceed = false;
   msg = "请输入用户名密码";
  }
  out.println(username);
  System.out.println(msg + isSucceed);
  System.out.println("登陆处理结束.........");
  if (isSucceed) {
   response.sendRedirect("succeed.jsp");
  } else {
   String url = "login.jsp?msg=" + URLEncoder.encode(msg, "GBK");
   response.sendRedirect(url);
  }
 }
}
******************************************************
login.jsp
*************************************
<%@ page language="java" pageEncoding="GB2312" %>
<%@ page import="java.net.URLDecoder"%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>login</title>
</head>
<%
request.setCharacterEncoding("GBK");
String msg=request.getParameter("msg");
if(msg!=null){
msg=URLDecoder.decode(msg,"GBK");
}else{
msg="";
}
%>
<body bgcolor="#FFFFFF">
<form action="LoginServlet" method="post">
<div>
<span style="width:70;">用户名:</span>
<span><input type="text" name="username"></span>
</div>
<div>

<span style="width:70;">密 码:</span>
<span><input type="password" name="password"></span>
</div>
<div>
<span style="width:70;"></span>
<span><input type="submit"></span>
</div>
<div style="color:red;">
<%
if(!msg.equals(""))
    out.println(msg);  
%>
</div>
</form>
</body>
</html>
******************************************
succeed.jsp 
***************************************************
<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>succeed</title>
</head>
<body bgcolor="#FFFFFF">
<%
Object username=session.getAttribute("username");
if(username!=null)
  out.println("用户名为:"+username);
else
  response.sendRedirect("login.jsp");
%>
</body>
</html>
************************************************
web.xml
******************************************************
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>Examples</display-name>
 <description>Examples. </description>
 <servlet>
  <servlet-name>LoginServlet</servlet-name>
  <servlet-class>model2.LoginServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/model2/LoginServlet</url-pattern>
 </servlet-mapping>
</web-app>
********************************************************

 

本文地址:http://com.8s8s.com/it/it12124.htm