JSP+Oracle简单实验

功能描述:后台建立的Oracle 10g数据库,客户端使用IE通过JSP服务器和JSP页面访问,实现用户登录。

环境描述:

  • 开发环境:Borland JBuilder 2005
  • 测试环境:Windows XP SP2,Tomcat 4.1,IE6,Oracle 10g个人版

参数:

  • 数据库用户jbuser(abc)默认表空间有用户数据表S_USER,用来验证登录合法性。
  • 登录页面:login.jsp,登陆验证页:chkpass.jsp,登陆成功:test.jsp,错误信息:error.jsp

项目结构如上。

代码如下:

  • 数据查询类DataQuery.java:

package jsporacletool;import java.sql.*;public class GetQuery
{
public static ResultSet DoGetQuery(String OraUser, String OraPass,
String Ex_SQL)
{
Connection conn = null;
try
{

Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:nocoo",
OraUser,
OraPass);

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(Ex_SQL);
return rs;
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
return null;
}
}
}

  • login.jsp:

@page contentType="text/html; charset=GBK"%>
<html>
<head>
<title>login</title>
</head>
<body bgcolor="#ffffff">
<h1>JBuilder Generated JSP</h1>
<form action="chkpass.jsp" method="POST">用户名:
<input name="name"/>
密码:
<input name="pass" type="password"/>
<input name="submit" value="登录" type="submit"/>
</form>
</body>
</html>

  • chkpass.jsp:

<%@page contentType="text/html; charset=GBK" import="jsporacletool.*,java.sql.*"%>
<html>
<head>
<title>chkpass</title>
</head>
<body bgcolor="#ffffff">
<h1>chkpass.jsp</h1>
<%
HttpSession session2 = request.getSession();
String name_temp = request.getParameter("name");
String pass_temp = request.getParameter("pass");
String remote_temp = request.getRemoteUser();
String request_url = request.getRequestURI();
out.println("name=" + name_temp + "<br>"
+ "pass=" + pass_temp + "<br>"
+ "remote=" + remote_temp + "<br>"
+ "request_url=" + request_url + "<br>");
ResultSet rs = null;
try {
rs = jsporacletool.GetQuery.DoGetQuery(
"jbuser", "abc", "SELECT * FROM S_user where user_name='" + name_temp + "'");
}
catch (Exception e) {
out.println("SQL error!");
}
if (rs == null) {
out.println("RS is NULL!!<br>");
%>
<jsp:forward page="error.jsp?error_reason=DB error or No such user!"></jsp:forward>
<%
} else {
out.println("RS is not NULL.<br>");
String current_pass = "";
if (rs.next()) {
current_pass = rs.getString("password");
}
if (current_pass.equals(pass_temp)) {
session2.setAttribute("cu_user",name_temp);
%>
<jsp:forward page="test.jsp"></jsp:forward>
<%} else {%>
<jsp:forward page="error.jsp?error_reason=Wrong Password!"></jsp:forward>
<%
}
}
%>
</body>
</html>

  • error.jsp:

<%@page contentType="text/html; charset=GBK"%>
<html>
<head>
<title>error</title>
</head>
<body bgcolor="#ffffff">
<h1>ERROR!!</h1>
<%
String error_reason = request.getParameter("error_reason");
if (error_reason != null) {
out.println("<br>The reason is:" + error_reason);
}
else {
out.println("<br>Unkown Reason!");
}
%>
</body>
</html>

  • test.jsp:

<%@page contentType="text/html; charset=GBK" import="jsporacletool.*,java.sql.*"%>
<html>
<head>
<title>test</title>
</head>
<body bgcolor="#c0c0c0">
<h1>JBuilder Generated JSP</h1>
<%
ResultSet rs = null;
try {
rs = jsporacletool.GetQuery.DoGetQuery("jbuser", "abc", "SELECT * FROM S_user where user_name='"+request.getParameter("c_user"+"'"));
}
catch (Exception e) {
out.println("SQL error!");
}
if (rs == null) {
out.println("RS is NULL!!<br>");
}
else {
out.println("RS is not NULL.<br>");
while (rs.next()) {
out.println(rs.getString("user_name") +"\t:"+ rs.getString("password") + "<br>");
}
}
HttpSession session3=request.getSession();
out.println("Your name="+(String)session3.getAttribute("cu_user"));
%>
</body>
</html>

推荐文章

发表评论