LoginServlet错在哪了
哪位高手帮忙看看,为何Login.jsp无法和LoginServlet交互,LoginServlet错在哪了
Login.jsp:
<%@ page language= "java " contentType= "text/html; charset=gb2312 "%>
<table align=center border=0 width=100% cellspacing=0 cellpadding=0 height=200>
<tr>
<td width=100% colspan=2 height=10 bgcolor=red>
<font color= "blue "> <center> 用户登录 </center> </font>
</td>
</tr>
<% if (request.getAttribute( "error ") != null){ %>
<tr>
<td width=100% align=center colspan=4 height=36>
<font color=red> <b> <%=request.getAttribute( "error ")%> </b> </font>
</td>
</tr>
<%
}
%>
<form name= "login " method= "post " action= "LoginServlet ">
<tr>
<td align=right height=32 width=20%> 用户名称: </td>
<td>
<input type= "text " name= "logname " value= " ">
</td>
</tr>
<tr>
<td align=right height=32> 密码: </td>
<td> <input type= "password " name= "logpwd "> </td>
</tr>
<tr>
<td align=center colspan=2 height=32>
<input type= "button " name= "login " value= "登 录 " onclick= "check() ">
<input type= "reset " name= "reset " value= "重 置 ">
</td>
<script language= "javascript ">
function check()
{
if(document.login.logname.value== " ")
{
window.alert( "请输入用户名! ");
window.login.logname.focus();
return false;
}
if(document.login.logpwd.value== " ")
{
window.alert( "请输入密码! ");
window.login.logpwd.focus();
return false;
}
login.submit();
}
</script>
</tr>
</form>
</table>
LoginServlet.java:
package Servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import JavaBean.user;
import JavaBean.DB_user;
public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
public LoginServlet()
{
super();
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType( "text/html;charset=GB2312 ");
String username=request.getParameter( "logname ");
String password=request.getParameter( "logpwd ");
DB_user dbuser = new DB_user();
user user=dbuser.get(username,password);
if(user!=null)
{
request.getSession(true).setAttribute( "username ",username);
try{
request.getRequestDispatcher( "index.jsp ").forward(request,response);
}
catch(Exception e){
System.out.println(e);
}
}
else
{
request.setAttribute( "error ", "用户名与密码不匹配,请重试! ");
try{
request.getRequestDispatcher( "Login.jsp ").forward(request,response);
}
catch(Exception e){
System.out.println(e);
}
}
}
}
DB_conn.java:
package JavaBean;
import java.sql.*;
import java.io.PrintWriter;
public class DB_conn{
private Connection conn=null;
public Statement sm=null;
private PrintWriter out = null;
public void connectDB(){
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
conn=DriverManager.getConnection( "jdbc:odbc:shop ", "sa ", " ");
sm=conn.createStatement();
}
catch(Exception e){
out.print( "数据库连接失败! ");
}
}
public void closeDB(){
try{
if(sm!=null)
{
sm.close();
}
conn.close();
}
catch(Exception e){
out.println( "数据库关闭失败! ");
}
}
}
DB_user.java:
package JavaBean;
import java.io.*;
import java.sql.*;
import java.util.*;
import JavaBean.user;
import JavaBean.DB_conn;
public class DB_user
{
DB_conn conn=new DB_conn();
public void add(user us)
{
conn.connectDB();
try{
String sql= "insert into userinfo(user_name,pwd, "
+ "email,gender,tel) values( ' " + us.getusername()
+ " ', ' " + us.getpwd() + " ', ' " + us.getemail()
+ " ', ' " + us.getgender() + " ', ' " + us.gettel()
+ " ') ";
conn.sm.executeUpdate(sql);
}
catch(Exception e){
System.out.println( "执行失败! ");
}
finally{
conn.closeDB();
}
}
public void update(user us)
{
conn.connectDB();
try {
String sql = "update userinfo set user_name= ' "
+ us.getusername() + " ',pwd= ' "
+ us.getpwd() + " ',email= ' "
+ us.getemail() + " ',gender= ' "
+ us.getgender() + " ',tel= ' "
+ us.gettel() + " ' where user_id= ' "
+ us.getuserid() + " ' ";
conn.sm.executeUpdate(sql);
}
catch (Exception e) {
System.out.println( "执行失败! ");
}
finally {
conn.closeDB();
}
}
public user get(String username, String password)
{
conn.connectDB();
ResultSet rs = null;
try {
String sql = "select * from userinfo where user_name = ' "
+ username + " ' and pwd= ' " + password + " ' ";
rs = conn.sm.executeQuery(sql);
user us = null;
if (rs.next())
{
us = new user();
us.setuserid(rs.getInt( "user_id "));
us.setusername(rs.getString( "username "));
us.setpwd(rs.getString( "pwd "));
us.setemail(rs.getString( "email "));
us.setgender(rs.getInt( "gender "));
us.settel(rs.getString( "tel "));
return us;
}
return us;
}
catch (Exception e) {
System.out.println(e);
return null;
} finally {
conn.closeDB();
}
}
}
user.java:
package JavaBean;
public class user{
private int userid;
private String username;
private String pwd;
private String email;
private int gender;
private String strgender;
private String tel;
public int getuserid() {
return userid;
}
public void setuserid(int id) {
this.userid=id;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username=username;
}
public String getpwd() {
return pwd;
}
public void setpwd(String pwd) {
this.pwd=pwd;
}
public String getemail() {
return email;
}
public void setemail(String email) {
this.email=email;
}
public int getgender() {
return gender;
}
public void setgender(int gender) {
this.gender=gender;
}
public String getstrgender(){
if (gender == 0)
strgender= "女 ";
else
strgender= "男 ";
return strgender;
}
public String gettel() {
return tel;
}
public void settel(String tel) {
this.tel=tel;
}
}
[解决办法]
这么多代码,错误却没有发布出来
[解决办法]
是呀 贴出来 大家帮你分析分析!
[解决办法]
用struts吧,
可以让你的开发简单的一塌糊涂。