jsp问题
下面是一个登陆数据库的页面 它调用了一个sqlbean.java 我应该怎样修改才把它们两个放到一个jsp 页面上让他有效运行呢 <%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="sqlbean" scope="page" class="database.sqlbean">
</jsp:useBean>
<html>
<head>
<title> 登录检查 </title>
</head>
<body>
<%
String userType = request.getParameter("userType");
String regName=sqlbean.AsciiToChineseString(request.getParameter("username").trim());
String regPassword=request.getParameter("password");
regPassword=regPassword.trim();
session.setAttribute("originname",regName);
if (userType.equals("Y"))
{
ResultSet rs=null;
String sqlstring = "select * from chatuser where username='"+ regName +"' and password='"+ regPassword + "'";;
rs = sqlbean.executeQuery(sqlstring);
if(rs.next())
{
String experience=rs.getString("experience");
String userlevel=rs.getString("userlevel");
session.setAttribute("experience",experience);
session.setAttribute("userlevel",userlevel);
regName = regName + "[会员]";
session.setAttribute("username",regName);
rs.close();
sqlbean.freeRs(rs);
response.sendRedirect ("chat.jsp");
}
else
{
rs.close();
out.print(" <script> alert(\"用户名/密码错误!\");");
out.print("window.location = \"login.jsp\" </script> ");
}
}
else
{
if((regName.length() <1) ¦ ¦(regName.length()> 10)) {
out.print(" <script> alert(\"游客名不能超过6个字长!\");window.close(); </script> ");
return;
}
Character c=new Character(' ');
for(int i=0;i <regName.length();i++) {
if(regName.charAt(i) == c.charValue()) {
out.print(" <script> alert(\"名字中间不能有空格!\");window.close(); </script> ");
return;
}
}
regName = regName + "[非会员]";
}
synchronized (application)
{
Vector UserName=null;
UserName= (Vector)application.getAttribute("UserName");
if(UserName==null) {
UserName= new Vector(30,10);
}
if(UserName.contains(regName)) {
out.print(" <script> alert(\"你的名字正在被人使用!\");window.close(); </script> ");
return;
}
if(regName!=null ) {
UserName.addElement(regName);
session.putValue("username", regName);
}
application.setAttribute("UserName",UserName);
out.print(" <script> window.location=\"chat.jsp\" </script> ");
}
%>
</body>
</html>
下面是那个sqlbean.java
package database;
import java.io.PrintStream;
import java.sql.*;
import sun.io.ByteToCharConverter;
public class sqlbean
{
private Connection con;
private ResultSet rs;
public sqlbean()
{
}
public static Connection getConnection()
throws SQLException
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
return null;
}
return DriverManager.getConnection("jdbc:mysql://localhost:3306/wsxk", "root", "1234567"));
}
public ResultSet executeQuery(String sql)
{
try
{
con = getConnection();
Statement statement = con.createStatement();
rs = statement.executeQuery(sql);
}
catch(SQLException sqlexception) { }
return rs;
}
public int executeUpdate(String sql)
{
int count = 0;
Statement stmt = null;
try
{
con = getConnection();
stmt = con.createStatement();
count = stmt.executeUpdate(sql);
}
catch(SQLException sqlexception) { }
finally
{
try
{
if(stmt != null)
{
stmt.close();
}
if(con != null)
{
con.close();
}
}
catch(SQLException ex)
{
System.err.print(ex);
}
}
return count;
}
public void freeRs(ResultSet rs)
{
try
{
if(rs != null)
{
rs.close();
con.close();
}
}
catch(Exception exception) { }
}
public static String AsciiToChineseString(String s)
{
char orig[] = s.toCharArray();
byte dest[] = new byte[orig.length];
System.out.println("111111");
System.out.println(s);
for(int i = 0; i < orig.length; i++)
{
dest[i] = (byte)(orig[i] & 0xff);
}
try
{
ByteToCharConverter toChar = ByteToCharConverter.getConverter("gb2312");
return new String(toChar.convertAll(dest));
}
catch(Exception exception)
{
System.out.println("222222222");
}
System.out.println(s);
return s;
}
}