jsp+servlet執行個體

來源:互聯網
上載者:User

開發環境myeclipse+tomcat6

1、先建立web project,項目名為RegisterSystem,

2、在WebRoot 目錄下建立login.jsp檔案,只需修改body中的內容,如下所示:

<body>
    <form action="login">
    username:<input type="text" name="username"><br>
    password:<input type="password" name="pwd"><br>
    <input type="submit">
    </form>
  </body>

3、在scr目錄下的com.ht.servlet編寫AcountBean.java檔案,代碼如下:

package com.ht.servlet;

public class AccountBean {
 private String username = "";
 private String password = "";
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
}

4、在scr目錄下的com.ht.servlet編寫servlet類CheckAccount.java檔案,代碼如下:

package com.ht.servlet;

import java.io.IOException;

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 CheckAccount extends HttpServlet {

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGet(req,resp);
 }

 @Override
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  HttpSession session = req.getSession();
  AccountBean account = new AccountBean();
  String username = req.getParameter("username");
  String pwd = req.getParameter("pwd");
  account.setPassword(pwd);
  account.setUsername(username);
  if((username != null)&&(username.trim().equals("jsp"))) {
   if((pwd != null)&&(pwd.trim().equals("1"))) {
    System.out.println("success");
    session.setAttribute("account", account);
    String login_suc = "success.jsp";
    resp.sendRedirect(login_suc);
    return;
   }
  }
  String login_fail = "fail.jsp";
  resp.sendRedirect(login_fail);
  return;
 }
 
}
5、在WebRoot目錄下編寫success.jsp檔案 成功後跳轉

<body>
    <%
    AccountBean account = (AccountBean)session.getAttribute("account");
    %>
    username:<%= account.getUsername()%>
     <br>
     password:<%= account.getPassword() %>
  </body>

6、在WebRoot目錄下編寫fail.jsp檔案 失敗後跳轉

<body>
   Login Failed! <br>
  </body>

7、修改web.xml設定檔

<?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">
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
       <description>This is the description of my J2EE component</description>
       <display-name>This is the display name of my J2EE component</display-name>
         <servlet-name>CheckAccount</servlet-name>
       <servlet-class>com.ht.servlet.CheckAccount</servlet-class>
     </servlet>

     <servlet-mapping>
       <servlet-name>CheckAccount</servlet-name>
       <url-pattern>/login</url-pattern>
     </servlet-mapping>

 
</web-app>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.