用servlet实现用户登录案例
以下实现登录窗口 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实现用户登录案例的更多相关文章
- Servlet——用户登录案例
案例:用户登录 * 用户登录案例需求: 1.编写login.html登录页面 username & password 两个输入框 2.使用Druid数据库连接池技术,操作mysql,day14 ...
- Servlet——简单用户登录实例+http协议解析
编写项目.用户登录系统1.0版本号 登录界面Servlet: package com.gavin.view; import java.io.IOException; import java.io.Pr ...
- Servlet+jsp用户登录加上验证码
最近公司有个项目被客户拿去进行漏洞扫描,发现用户登录太简单,容易被暴力破解.当然发现的问题很多,什么反射型XSS,存储型XSS,敏感信息泄露等等.但是我们今天不讲这么多,就说说如何修复暴力破解的问题. ...
- Servlet实现用户登录
1.登录过程分析: 通过表单收集用户的数据,Servlet通过request对象获得用户提交的数据,服务器还需要从数据库中通过sql语句查询有没有表单提交的数据中的用户.有则登录成功,否则,登录失败. ...
- JavaWeb 之 Filter 验证用户登录案例
需求: 1. 访问一个网站的资源.验证其是否登录 2. 如果登录了,则直接放行. 3. 如果没有登录,则跳转到登录页面,提示"您尚未登录,请先登录". 代码实现: import j ...
- Java EE之servlet实现用户登录
1.在连接数据库的JAVA类中添加查询功能: 在这之前有一个连接数据库的方法: Connection conn=null; PreparedStatement stat=null; ...
- jsp使用servlet实现用户登录 及动态验证码
在进行表单设计中,验证码的增加恰恰可以实现是否为“人为”操作,增加验证码可以防止网站数据库信息的冗杂等... 现在,我将讲述通过servlet实现验证码: 验证码作为一个图片,在页面中为“画”出来的, ...
- SSM整合案例--用户登录
实现用户登录案例,并进行非法拦截 实现当用户未登录时,无法跳转到出登录页面以外的任何页面,拦截用户仍在登陆页面:当用户登录成功即可跳转到其他页面 (1)导入依赖 <!-- https://mvn ...
- Java 之 request 案例——用户登录
用户登录案例需求: 1.编写login.html登录页面 username & password 两个输入框 2.使用Druid数据库连接池技术,操作mysql,db1数据库中user表 3. ...
随机推荐
- Java基础加强总结(一)——注解(Annotation)
一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts ...
- 关于如何获取Google 官方 NavgationView中的控件的方法
最近在想要寻找一个好集成的SlidingMenu,看上了官方的DrawLayout,简单易集成 然后如果想动态改变DrawLayout头部的HeaderVIew的资源的话需要先获取到控件 方法如下 n ...
- MVC实战之排球计分(四)—— View设计与实现
(view)视图 视图是用户看到并与之交互的界面.对老式的Web应用程序来说,视图就是由HTML元素组成的界面,在新式的Web应用程序中,HTML依旧在视图中扮演着重要的角色,但一些新的技术已层出不穷 ...
- 【MySQL】【3】String和Date相互转换
正文: 1,Date转String --结果:<2019-04-10> SELECT DATE_FORMAT(SYSDATE(), "%Y-%m-%d") FROM D ...
- leetcode-algorithms-36 Valid Sudoku
leetcode-algorithms-36 Valid Sudoku Determine if a 9x9 Sudoku board is valid. Only the filled cells ...
- Java 8 forEach
1. forEach and Map 1.1 通常这样遍历一个Map Map<String, Integer> items = new HashMap<>(); items.p ...
- jps报process information unavailable的解决办法
现象 启动Hadoop的时候使用jps检查进程 ,出现Process information unavailable的问题,如下 [root@vm8033 local]# jps -- process ...
- 1.11 UML 类图(多看多用就熟悉了)(节选自:《大话设计模式》)
类:用矩形框表示(类图分三层) 第一层显示类的名称:(如果是抽象类,就用斜体显示) 第二层是类的特性,通常就是字段和属性: 第三层是类的操作,通常是方法或行为. (注意前面的符号,“+” 表示 pub ...
- SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI(转)
原文地址:SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI 采用 spring 零配置,参考 http://hanqunf ...
- 关于RabbitMQ服务器整合(二)
准备工作 15min IDEA maven 3.0 在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/download.html ,如 ...