• 简单jsp+servlet实例


    开发环境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

  • 相关阅读:
    VS2010 LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 的解决方法
    Navicat Premium11.0.16 for mac 中文破解版
    angular input输入框中使用filter格式化日期
    Mac下搭建Eclipse Android开发环境
    mac下修改.bash_profile立即生效的方法
    Ionic ngMessage 表单验证
    mongodb授权登录
    Ionic开发之条形码扫描
    ionic 到真相后$http.get()无法请求,导致空白的情况,如何解决
    Xcode 7中http通信出现如下错误:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
  • 原文地址:https://www.cnblogs.com/xinxindiandeng/p/1249571.html
Copyright © 2020-2023  润新知