主题:一个简单的问题,请高手看看
我写了一个servlet接受和处理用户输入的信息,但总是返回null,不知道那点错了,请高手看看,访问的时候不报任何错误,就是打印出来总是null
Conn.java文件如下
package classes.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn
{
private static Connection conn;
private Conn()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/news","root","123123");
}catch(Exception e)
{
}
}
public static Connection getConnection()
{
if(conn==null)
{
new Conn();
}
return conn;
}
}
LoginBean.java文件如下
package classes.login;
import java.sql.*;
import classes.conn.Conn;
public class LoginBean
{
public LoginBean()
{
}
public String loginYanZheng(String name,String pwd)
{
String result = null;
Connection conn = Conn.getConnection();
Statement statement = null;
ResultSet rs = null;
try{
statement = conn.createStatement();
rs = statement.executeQuery("select userpwd from user where username='"+name+"'");
if (rs.next())
{
if(pwd.equals(rs.getString(1)))
{
result="ok";
}else
{
result="pwd is wrong";
}
}else
{
result="nouser";
}
try
{
if(rs!=null)rs.close();
if(statement!=null)statement.close();
if(conn!=null)conn.close();
}catch(Exception e)
{
}
}catch(Exception e)
{
try
{
if(rs!=null) rs.close();
if(statement!=null) statement.close();
if(conn!=null) conn.close();
}catch(Exception ee)
{
}
}
return result;
}
}
LoginServlet.java文件如下
package servlet.login;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import classes.login.LoginBean;
public class LoginServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
LoginBean lb=new LoginBean();
String result=lb.loginYanZheng(name,pwd);
out.println("name="+name);
out.println("pwd="+pwd);
out.println("result="+result);
/*
if(result=="ok")
{
out.println("ok");
}else if(result=="nouser")
{
out.println("no user");
}else if(result=="pwd is wrong")
{
out.println("pwd wrong");
}
*/
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doPost(request,response);
}
}
在浏览器访问时没有任何错误,但result的值总是打印出null,而name和pwd没有任何问题
文件组织是这样的在classes目录下面有classes目录和servlet目录
有下面的Test.java测试没有任何问题
import java.util.*;
import classes.login.LoginBean;
public class Test
{
public static void main(String[] args)
{
LoginBean lb=new LoginBean();
String result=lb.loginYanZheng("hushan","12");
System.out.println(result);
}
}
Web.xml文件里是这样的
<servlet>
<servlet-name>loginservlet</servlet-name>
<servlet-class>servlet.login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginservlet</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
Conn.java文件如下
package classes.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn
{
private static Connection conn;
private Conn()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/news","root","123123");
}catch(Exception e)
{
}
}
public static Connection getConnection()
{
if(conn==null)
{
new Conn();
}
return conn;
}
}
LoginBean.java文件如下
package classes.login;
import java.sql.*;
import classes.conn.Conn;
public class LoginBean
{
public LoginBean()
{
}
public String loginYanZheng(String name,String pwd)
{
String result = null;
Connection conn = Conn.getConnection();
Statement statement = null;
ResultSet rs = null;
try{
statement = conn.createStatement();
rs = statement.executeQuery("select userpwd from user where username='"+name+"'");
if (rs.next())
{
if(pwd.equals(rs.getString(1)))
{
result="ok";
}else
{
result="pwd is wrong";
}
}else
{
result="nouser";
}
try
{
if(rs!=null)rs.close();
if(statement!=null)statement.close();
if(conn!=null)conn.close();
}catch(Exception e)
{
}
}catch(Exception e)
{
try
{
if(rs!=null) rs.close();
if(statement!=null) statement.close();
if(conn!=null) conn.close();
}catch(Exception ee)
{
}
}
return result;
}
}
LoginServlet.java文件如下
package servlet.login;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import classes.login.LoginBean;
public class LoginServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
LoginBean lb=new LoginBean();
String result=lb.loginYanZheng(name,pwd);
out.println("name="+name);
out.println("pwd="+pwd);
out.println("result="+result);
/*
if(result=="ok")
{
out.println("ok");
}else if(result=="nouser")
{
out.println("no user");
}else if(result=="pwd is wrong")
{
out.println("pwd wrong");
}
*/
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doPost(request,response);
}
}
在浏览器访问时没有任何错误,但result的值总是打印出null,而name和pwd没有任何问题
文件组织是这样的在classes目录下面有classes目录和servlet目录
有下面的Test.java测试没有任何问题
import java.util.*;
import classes.login.LoginBean;
public class Test
{
public static void main(String[] args)
{
LoginBean lb=new LoginBean();
String result=lb.loginYanZheng("hushan","12");
System.out.println(result);
}
}
Web.xml文件里是这样的
<servlet>
<servlet-name>loginservlet</servlet-name>
<servlet-class>servlet.login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginservlet</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>