JSP+JDBC

2018-08-27 10:32:51 浏览数 (1)

login.jsp

代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登陆界面</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form action="validate.jsp" method="post">
    	用户登录<br>
    	姓名:<input type="text" name="username"/><br>
    	密码:<input type="text" name="pswd"/><br>
    	<input type="submit" value="登录"/>
    </form>
  </body>
</html>

validate.jsp

代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.sql.*" %>
<jsp:useBean id="MySqlBean" scope="page" class="edu.qdgxy.jdbc.MySQLConnBean" />
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'validate.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
  </head>
  
  <body>
  	<%
  		String username=request.getParameter("username");
  		String pswd=request.getParameter("pswd");
  		boolean validated=false;
  		String sql="select * from user";
  		MySqlBean.OpenConn();
  		ResultSet rs=MySqlBean.executeQuery(sql);
  		while(rs.next()){  			
  			if((rs.getString("username").compareTo(username)==0)&&(rs.getString("pswd").compareTo(pswd)==0)){
  				validated=true;
  			}
  		}
  		rs.close();
  		MySqlBean.closeStmt();
  		MySqlBean.closeConn();
  		if(validated){
  	 %>
   <jsp:forward page="welcome.jsp"/>
   		<%
   			}
   			else{
   		%>
   		<jsp:forward page="error.jsp"/>
   		<% 
   			}
   		 %>
  </body>
</html>

MySQLConnBean.java请参考http://blog.csdn.net/jxq0816/article/details/20442603

0 人点赞