zoukankan      html  css  js  c++  java
  • JSP+Servlet 无数据库模拟登录过程

    程序目录结构:

    index.jsp:

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <%@ page import="java.io.*,java.util.*" %>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     8 <title>Insert title here</title>
     9 </head>
    10 
    11 <style>
    12 </style>
    13 <body>
    14     <form action="index" method="post">
    15         <input type="text" name="txtName" value="用户名" maxlength="12">
    16         <input type="password" name="passwd" value="请输入密码" maxlength="12">
    17         <input type="submit" name="btnSubmit" value="提交">
    18     </form>
    19 </body>
    20 </html>

    servlet 代码:

     1 package servlet;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.Servlet;
     6 import javax.servlet.ServletConfig;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.ServletRequest;
     9 import javax.servlet.ServletResponse;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 public class loginServlet implements Servlet {
    14 
    15     @Override
    16     public void destroy() {
    17         // TODO Auto-generated method stub
    18 
    19     }
    20 
    21     @Override
    22     public ServletConfig getServletConfig() {
    23         // TODO Auto-generated method stub
    24         return null;
    25     }
    26 
    27     @Override
    28     public String getServletInfo() {
    29         // TODO Auto-generated method stub
    30         return null;
    31     }
    32 
    33     @Override
    34     public void init(ServletConfig arg0) throws ServletException {
    35         // TODO Auto-generated method stub
    36 
    37     }
    38 
    39     @Override
    40     public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    41         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    42         HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    43         String userName = httpServletRequest.getParameter("txtName");
    44         String userPwd = httpServletRequest.getParameter("passwd");
    45         if (userName.equals("g.qu") && userPwd.equals("g.qu")) {
    46             httpServletResponse.sendRedirect("Hello.html");
    47         } else {
    48             httpServletResponse.sendRedirect("main.jsp");
    49         }
    50     }
    51 }

    web.xml 基础代码:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     5     id="WebApp_ID" version="3.0">
     6     <!-- Web程序名称 -->
     7     <display-name>MyWeb</display-name>
     8 
     9     <!-- 应用程序的描述信息 -->
    10     <description></description>
    11 
    12     <!-- 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面 -->
    13     <servlet>
    14         <!-- Servlet名字,可以随便取,有多个Servlet时不允许重名 -->
    15         <servlet-name>loginServlet</servlet-name>
    16         <!-- 指定实现这个Servlet的类。完整的包名+类名 -->
    17         <servlet-class>servlet.loginServlet</servlet-class>
    18     </servlet>
    19 
    20     <!-- 服务器一般为servlet提供一个缺省的URL;但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。 
    21         在更改缺省URL时,使用servlet-mapping元素。 -->
    22     <servlet-mapping>
    23         <!-- 必须和<servlet>里的<servlet-name>内容一样 -->
    24         <servlet-name>loginServlet</servlet-name>
    25         <!-- 指定访问这个Servlet的URL,这里给出的是对于整个Web应用的相对URL路径 -->
    26         <url-pattern>/index</url-pattern>
    27     </servlet-mapping>
    28 
    29     <!-- 在 用户访问Web应用时,如果仅给出Web应用的根访问URL,没有指定具体的文件名,容器会调用<weblcome-file- list> 
    30         元素里指定的文件清单。<welcome-file-list>里允许有多个<welcome-file>元 素,每个元素代表一个文件。 容器会先找第一文文件件是否存在,如果存在这把这个文件返回个客户,不再进行其他文件的查找。如果不存在则找第二个文件,依次 
    31         类推。 如果所有文件都不存在,则跑出404错误 -->
    32     <welcome-file-list>
    33         <welcome-file>index.jsp</welcome-file>
    34     </welcome-file-list>
    35 
    36     <!-- 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。 -->
    37     <error-page>
    38         <error-code>404</error-code>
    39         <location>/error.html</location>
    40     </error-page>
    41 
    42     <!-- 设 定HttpSession的生命周期。这里以分钟计算。下面的设定指明Session在最长不活动时间为10分钟。 过了这个时间,Servlet容器将它 
    43         作为无效处理。注意这里和程序里指定的计数单位不同,程序里是以秒为单位。 -->
    44     <session-config>
    45         <session-timeout>10</session-timeout>
    46     </session-config>
    47 
    48     <filter>
    49         <!-- 过滤器名,可以随便取,当web应用中有多个过滤器时不允许重名. -->
    50         <filter-name>SampleFilter</filter-name>
    51         <!-- 具体的过滤器的类的完整的包名+类名。注意:不能写错了。否则容器不能正确的实例化过滤器 -->
    52         <filter-class></filter-class>
    53     </filter>
    54 
    55 <!-- 如果要想进行认证,必须有login-config节点  -->
    56         <login-config> 
    57 <!--认证方式。有4种:BASIC:基本。 DIGEST:摘要。CLIENT-CERT:客户证书(能提供最高强度的认证)。FORM:表单  -->
    58             <auth-method>FORM</auth-method>
    59             <realm-name>
    60                 Tomcat Servet Configuraton Form-Based Authentication Area
    61             </realm-name>
    62             <form-login-config>
    63                 <form-login-page>/login.jsp</form-login-page>
    64                 <form-error-page>/error.jsp</form-error-page>
    65             </form-login-config>
    66         </login-config>
    67 </web-app>

    启动Tomcat 服务器;在浏览器中输入地址  http://localhost:8080/MyWeb/index.jsp 访问

  • 相关阅读:
    app测试和web测试的相同点和不同点
    重写 和 重载
    23种设计模式总概括
    小程序webview跳转页面后没有返回按钮完美解决方案
    服务器配置https协议,三种免费的方法
    Android通过外部浏览器调用微信H5支付,Android+PHP详解
    vue多级复杂列表展开/折叠,全选/分组全选实现
    A very good RPC debugging session
    在WinForms中使用ActiveX
    Lauch Debugger automatically when start the process
  • 原文地址:https://www.cnblogs.com/QQ931697811/p/5016376.html
Copyright © 2011-2022 走看看