B/S登陆例子(JSP+MYSQL)

本文介绍了一个基于JSP的简易登录系统的实现过程。该系统通过web.xml进行配置,并使用了index.jsp、login.jsp和loginSuccess.jsp三个页面。用户输入用户名和密码后,系统将验证这些信息并重定向到相应的页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  
<display-name>jspLogin</display-name>
</web-app>

 

 index.jsp

 

 <%
 
if(session.getAttribute("passed")==null){
  response.sendRedirect(
"login.jsp");
  
return;
 }

%>
passed
!

 

 login.jsp

 

<%@ page contentType="text/html; charset=utf-8" %>
<html>
<head>
<title>
login
</title>
</head>
<body bgcolor="#ffffff">
<%
String flag=request.getParameter("flag");
if(flag!=null&&flag.equals("0")){
  out.println(
"Pls input valid username and password!<br><br>");
}
%>
<form name="form1" method="post" action="loginSuccess.jsp">
<input name="username" type="text"><br/>
<input name="password" type="password"><br/>
<input name="reset" type="reset" value="reset">
<input name="submit" type="submit" value="submit">
</form>
</body>
</html>

 

 

loginSuccess.jsp

 

<%@ page import="java.sql.*"%>
<%
String username
=request.getParameter("username");
String password
=request.getParameter("password");
String sql
="select * from user where username='"+username+"' and password='"+password+"'";
boolean flag=false;
//
Class.forName("com.mysql.jdbc.Driver") ;
Connection conn 
= DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=cwb&password=cwb123&useUnicode=true");
Statement pstmt 
= conn.prepareStatement(sql) ;
ResultSet rs 
= pstmt.executeQuery(sql);

if(rs.next()) {
flag 
= true ;
}

//
rs.close() ;
pstmt.close() ;
conn.close() ;

if(flag){
  session.setAttribute(
"passed","true");
  response.sendRedirect(
"index.jsp");
}
else{
  response.sendRedirect(
"login.jsp?flag=0");
}

%>

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值