簡單jsp+servlet執行個體

來源:互聯網
上載者:User

開發環境myeclipse+tomcat5

先建立web project,項目名為test,

在webroot 目錄下建立login.jsp檔案

login.jsp

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

在scr目錄下的com.ht.servlet

編寫AcountBean.java檔案

package com.ht.servlet;

public class AcountBean {
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;
}

}

編寫servlet類CheckAccount.java檔案

package com.ht.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.*;

public class CheckAccount extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
     HttpSession session = request.getSession();
     AcountBean account = new AcountBean();
     String username = request.getParameter("username");
     String pwd = request.getParameter("pwd");
     account.setUsername(username);
     account.setPassword(pwd);
     if ((username != null) && (username.trim().equals("jsp"))) {
      if ((pwd != null) && (pwd.trim().equals("1"))) {
       System.out.println("session");
       session.setAttribute("account", account);
       String logon_suc = "session.jsp";
       response.sendRedirect(logon_suc);
       return;
      }
     }
     String logon_fail = "fail.jsp";
     response.sendRedirect(logon_fail);
     return;
}

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

     doGet(request, response);
}

}

在webroot目錄下

編寫session.jsp檔案 成功後跳轉

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

編寫fail.jsp檔案 失敗後跳轉

<body>
       Logon Failed <br>
     </body>

下面是web.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>

通過myeclipse運行 配置tomcat     這裡省略。。。

http://127.0.0.1:8080/tset/ 驗證察看結果

源碼下載http://tel4.800disk.com/?mbsky0618 密碼mbsky0618

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.