用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. ...
随机推荐
- 单分子荧光原位杂交(smFISH)
single-molecule RNA fluorescence in situ hybridization (RNA smFISH) 单分子荧光原位杂交(smFISH)是一种新的基因表达分析方法,能 ...
- java---->Itellij idea报错:错误: 找不到或无法加载主类 main
没有设置好正确的类路径 点击上面圈红色处,在点击Edit Configuration,进入下面设置界面 切换到下面这个界面 红色×消失,运行正常,截图如下
- Centos6.8 smokeping安装
yum -y install rrdtool perl-rrdtool curl perl-core bind bind-chroot bind-utils httpd popt popt-devel ...
- Confluence 6 编辑一个空间的配色方案
空间默认继承全局的配色方案.但是,如果你是空间管理员的话,你可以对默认继承的全局方案进行调整,使用自定义的配色方案. 为一个空间修改配色方案: 进入空间后,然后从边栏的底部选择 空间工具(Space ...
- Routing a Marathon Race
直接爆搜的复杂度是2^n,对于n<=40的数据过不了. 考虑优化一下. 发现如果走了一个点后,以后是不可能再经过与它相邻的点的,因为这样走显然不如直接走那个与它相邻的点. 这样每走一步就可以删掉 ...
- 第一章:IPsecVPN
第一章 一.VPN(virtual private Network,虚拟专用网)的基本概念 VPN连接模式分为两种,分别是传输模式和隧道模式 传输模式:在整个VPN传输中,ip包头并没有被封装进去 隧 ...
- 自动化(脚本)安装httpd服务
思路: 1.检查传递的参数,httpd源码文件 2.检查执行脚本的用户是否为root 3.检查rpm是否安装过httpd,若安装过,则卸载 4.安装编译所需的工具 5.从网上下载httpd源码 6.配 ...
- vue 监听store中的数值
computed: { isFollow () { return this.$store.state.demo.id; //需要监听的数据 } }, watch: { isFo ...
- leetcode-algorithms-23 Merge k Sorted Lists
leetcode-algorithms-23 Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted ...
- 函数使用五:MIR7 发票预制 BAPI_INCOMINGINVOICE_PARK
引自:http://blog.csdn.net/champaignwolf/article/details/51422329 FUNCTION zincominginvoice_park. *&quo ...