//登陆页面
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>bookStore</title>
</head>
<body>
<h1>欢迎光临</h1>
<form action="sucess" method="POST">
<table border="1">
<tbody>
<tr>
<td>用户名:<input type="text" name="user"></td>
</tr>
<tr>
<td>密码 :<input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="提交" /> <A href="register.html">还没注册</A></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
//注册页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
//<html lang='zh'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>注册页面</title>
</head>
<body>
<form action="register" method="POST">
<table border="1">
<tbody>
<tr>
<td>用户名:</td>
<td><input type="text" name="user" value="" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password1" value="" /></td>
</tr>
<tr>
<td>核对密码:</td>
<td><input type="password" name="password2" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
<td><input type="submit" value="重填" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
//注册处理
/*
* Register.java
*
* Created on 2007年9月25日, 上午8:11
*/
package moon;
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Register extends HttpServlet {
String driverClass;
String url;
String user;
String password;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("text/html;charset=GBK");
String userName = request.getParameter("user");
String userPassword1 = request.getParameter("password1");
String userPassword2 = request.getParameter("password2");
String userPassword = null;
initJDBC();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM user");
}
catch(Exception e)
{
e.printStackTrace();
}
if(userPassword1!=userPassword2)
{
out.print("<h1>2次输入密码不一致</h1>");
}
else if(userPassword1==userPassword2)
{
userPassword = userPassword1;
}
else if(userName==null)
{
out.print("<h1>用户名不能为空</h1>");
}
else
{
try
{
while(rs.next())
{
if(rs.getString("user").equals(user))
{
out.print("<h1>用户已被注册</h1>");
}
else
{
stmt.executeUpdate("INSERT INTO user VALUES (userName,userPassword)");
out.println("<h1>注册成功</h1>");
out.println("<h2>你的用户名为:"+userName+"</h2>");
out.println("<h2>你的密码为:"+userPassword+"</h2>");
}
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
out.close();
}
private void initJDBC()
{
ServletContext context = getServletContext();
driverClass = context.getInitParameter("driverClass");
url = context.getInitParameter("url");
user = context.getInitParameter("password");
password = context.getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
//登陆处理
/*
* Scuess.java
*
* Created on 2007年9月25日, 上午9:23
*/
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Scuess extends HttpServlet {
String driverClass;
String url;
String user;
String password;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("text/html;charset=GBK");
String userName = request.getParameter("user");
String userPassword = request.getParameter("password");
initJDBC();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM user");
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if(rs.getString("user").equals(user)&&rs.getString("password").equals(userPassword))
{
out.println("<h1>欢迎你"+userName+"</h1>");
}
else
{
out.println("<h1>输入的用户名或者密码有误</h1>");
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
out.close();
/* finally
{
if(rs!=null)
rs.close(); //为什么一执行这里就有问题
if(stmt!= null);
stmt.close();
if(conn!= null)
conn.close();
}
}*/
}
private void initJDBC()
{
ServletContext context = getServletContext();
driverClass = context.getInitParameter("driverClass");
url = context.getInitParameter("url");
user = context.getInitParameter("password");
password = context.getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
//程序出不来,不知道什么原因,我是跑在netBeans上的,连JSP都跑不起来提示我错误500,高手帮看下