您的位置:首页 > 数据库

JSP+Servlet 无数据库模拟登录过程

2015-12-03 15:24 246 查看
程序目录结构:





index.jsp:

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<style>
</style>
<body>
<form action="index" method="post">
<input type="text" name="txtName" value="用户名" maxlength="12">
<input type="password" name="passwd" value="请输入密码" maxlength="12">
<input type="submit" name="btnSubmit" value="提交">
</form>
</body>
</html>


servlet 代码:

 1 package servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class loginServlet implements Servlet {

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}

@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String userName = httpServletRequest.getParameter("txtName");
String userPwd = httpServletRequest.getParameter("passwd");
if (userName.equals("g.qu") && userPwd.equals("g.qu")) {
httpServletResponse.sendRedirect("Hello.html");
} else {
httpServletResponse.sendRedirect("main.jsp");
}
}
}


web.xml 基础代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- Web程序名称 -->
<display-name>MyWeb</display-name>

<!-- 应用程序的描述信息 -->
<description></description>

<!-- 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面 -->
<servlet>
<!-- Servlet名字,可以随便取,有多个Servlet时不允许重名 -->
<servlet-name>loginServlet</servlet-name>
<!-- 指定实现这个Servlet的类。完整的包名+类名 -->
<servlet-class>servlet.loginServlet</servlet-class>
</servlet>

<!-- 服务器一般为servlet提供一个缺省的URL;但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。
在更改缺省URL时,使用servlet-mapping元素。 -->
<servlet-mapping>
<!-- 必须和<servlet>里的<servlet-name>内容一样 -->
<servlet-name>loginServlet</servlet-name>
<!-- 指定访问这个Servlet的URL,这里给出的是对于整个Web应用的相对URL路径 -->
<url-pattern>/index</url-pattern>
</servlet-mapping>

<!-- 在 用户访问Web应用时,如果仅给出Web应用的根访问URL,没有指定具体的文件名,容器会调用<weblcome-file- list>
元素里指定的文件清单。<welcome-file-list>里允许有多个<welcome-file>元 素,每个元素代表一个文件。 容器会先找第一文文件件是否存在,如果存在这把这个文件返回个客户,不再进行其他文件的查找。如果不存在则找第二个文件,依次
类推。 如果所有文件都不存在,则跑出404错误 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。 -->
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>

<!-- 设 定HttpSession的生命周期。这里以分钟计算。下面的设定指明Session在最长不活动时间为10分钟。 过了这个时间,Servlet容器将它
作为无效处理。注意这里和程序里指定的计数单位不同,程序里是以秒为单位。 -->
<session-config>
<session-timeout>10</session-timeout>
</session-config>

<filter>
<!-- 过滤器名,可以随便取,当web应用中有多个过滤器时不允许重名. -->
<filter-name>SampleFilter</filter-name>
<!-- 具体的过滤器的类的完整的包名+类名。注意:不能写错了。否则容器不能正确的实例化过滤器 -->
<filter-class></filter-class>
</filter>

<!-- 如果要想进行认证,必须有login-config节点  -->
<login-config>
<!--认证方式。有4种:BASIC:基本。 DIGEST:摘要。CLIENT-CERT:客户证书(能提供最高强度的认证)。FORM:表单  -->
<auth-method>FORM</auth-method>
<realm-name>
Tomcat Servet Configuraton Form-Based Authentication Area
</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
</web-app>


启动Tomcat 服务器;在浏览器中输入地址 http://localhost:8080/MyWeb/index.jsp 访问
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: