以下实现登录窗口 Login.jsp

<!--Login.jsp-->
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html>
<head> <title>登录页面</title> </head> <body bgcolor="cccfff"> <form action="Check" method="post">
<table>
<tr><td>用户</td><td><input type="text"name="user"></td></tr>
<tr><td>密码</td><td><input type="password"name="password"></td></tr>
<tr align="center">
<td colspan="2">
  <input type="submit"value="登 录">
   
<input type="reset"value="取 消 ">
</td>
</tr>
</table>
</form>
</body>
</html>

  

处理登录的Servlet

package ch04;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Check extends HttpServlet { /**
* Constructor of the object.
*/
public Check() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
/*
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
*/ /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/ public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String name=request.getParameter("user");//获取用户名
String password=request.getParameter("password");//获取密码 if(("client".equals(name))&&"123456".equals(password)){//设置用户名和密码
//如果用户名和密码相对应,则跳转到学生体质管理页面
RequestDispatcher rd=request.getRequestDispatcher("success.jsp");
rd.forward(request, response); }else{//账户名或密码不正确则跳转登录失败页面 RequestDispatcher rd=request.getRequestDispatcher("Faile.jsp");
rd.forward(request, response);
} } /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }

登录成功页面success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head> <title>成功页面</title> </head> <body>
<%String Name=request.getParameter("user"); %>
欢迎,<%=Name %>成功登陆!
</body>
</html>

  

登录失败页面Faile.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head> <title>失败页面</title> </head> <body>
登陆失败!
<!--获取用户名 -->
<%String Name=request.getParameter("user"); %>
<!--重新跳转到登陆页面 -->
<br><a href="Login.jsp">请重新登录,<%=Name %>同学!
</body>
</html>

  

配置文件,在web.xml中,添加Check的配置信息,

(注意jsp页面调用Servlet的方法)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<servlet> <servlet-name>Check</servlet-name>
<servlet-class>ch04.Check</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Check</servlet-name>
<url-pattern>/Check</url-pattern>
</servlet-mapping> </web-app>

  

用servlet实现用户登录案例的更多相关文章

  1. Servlet——用户登录案例

    案例:用户登录 * 用户登录案例需求: 1.编写login.html登录页面 username & password 两个输入框 2.使用Druid数据库连接池技术,操作mysql,day14 ...

  2. Servlet——简单用户登录实例+http协议解析

    编写项目.用户登录系统1.0版本号 登录界面Servlet: package com.gavin.view; import java.io.IOException; import java.io.Pr ...

  3. Servlet+jsp用户登录加上验证码

    最近公司有个项目被客户拿去进行漏洞扫描,发现用户登录太简单,容易被暴力破解.当然发现的问题很多,什么反射型XSS,存储型XSS,敏感信息泄露等等.但是我们今天不讲这么多,就说说如何修复暴力破解的问题. ...

  4. Servlet实现用户登录

    1.登录过程分析: 通过表单收集用户的数据,Servlet通过request对象获得用户提交的数据,服务器还需要从数据库中通过sql语句查询有没有表单提交的数据中的用户.有则登录成功,否则,登录失败. ...

  5. JavaWeb 之 Filter 验证用户登录案例

    需求: 1. 访问一个网站的资源.验证其是否登录 2. 如果登录了,则直接放行. 3. 如果没有登录,则跳转到登录页面,提示"您尚未登录,请先登录". 代码实现: import j ...

  6. Java EE之servlet实现用户登录

    1.在连接数据库的JAVA类中添加查询功能: 在这之前有一个连接数据库的方法: Connection conn=null; PreparedStatement stat=null;           ...

  7. jsp使用servlet实现用户登录 及动态验证码

    在进行表单设计中,验证码的增加恰恰可以实现是否为“人为”操作,增加验证码可以防止网站数据库信息的冗杂等... 现在,我将讲述通过servlet实现验证码: 验证码作为一个图片,在页面中为“画”出来的, ...

  8. SSM整合案例--用户登录

    实现用户登录案例,并进行非法拦截 实现当用户未登录时,无法跳转到出登录页面以外的任何页面,拦截用户仍在登陆页面:当用户登录成功即可跳转到其他页面 (1)导入依赖 <!-- https://mvn ...

  9. Java 之 request 案例——用户登录

    用户登录案例需求: 1.编写login.html登录页面 username & password 两个输入框 2.使用Druid数据库连接池技术,操作mysql,db1数据库中user表 3. ...

随机推荐

  1. 皮尔逊残差 | Pearson residual

    参考:Pearson Residuals 这些概念到底是写什么?怎么产生的? 统计学功力太弱了!

  2. C#定义一个方法的3种形式

    [定义方法对象,可使用签名兼容的委托变量来引用它] 1.定义一个方法,采用常规标准写法(V1.0) [本质上是签名兼容的委托类型的实例 / 对象] 2.定义一个方法,采用匿名方法(delegate)( ...

  3. Debian初始化配置

    1.解决中文显示乱码windows的宋体文件上传到debian的字体目录,并运行dpkg-reconfigure locales命令来设置系统的字体root@debian:~# mv simsun.t ...

  4. PHP多种序列化/反序列化的方法(serialize和unserialize函数)

    serialize和unserialize函数 这两个是序列化和反序列化PHP中数据的常用函数. <?php $a = array('a' => 'Apple' ,'b' => 'b ...

  5. 第二阶段——个人工作总结DAY02

    1.昨天做了什么:昨天学习了Intent跳转的知识. 2.今天打算做什么:来实现这个功能. 3.遇到的困难:不会用隐式跳转,只会用显式跳转.

  6. WDA基础一:激活相关服务

    一个普通得系统,如果之前没动过相关配置而又想做做WDA程序,是需要激活几个服务的. 1.激活服务 事务码:SICF 默认SERVICE,执行. Service:default_host/sap/opt ...

  7. 解决maven 找不到指定路径应该如何

    如果打开每个项目都有红x!!!解决办法: 1) 2)-->点击clean ,ok!!

  8. android project

  9. Nginx基本

    静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端.配置: server { listen ; # 端口号 locati ...

  10. CSS三列布局之左右宽度固定,中间元素自适应问题

    最近学到了几种关于左右固定宽度,中间自适应的三列布局的方法,整理了一下,在这里跟大家一起分享分享,其中有什么不足的还望各位给指导指导哈. 首先我想到的是float——浮动布局 使用浮动,先渲染左右两个 ...