当前位置: 代码迷 >> Java Web开发 >> 在servlet中调用访问数据库的bean返回空值
  详细解决方案

在servlet中调用访问数据库的bean返回空值

热度:182   发布时间:2006-09-12 21:56:40.0
在servlet中调用访问数据库的bean返回空值

这是我的servlet
import Bean.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import Bean.Customer;

public class ViewServlet extends HttpServlet
{
private Bean.Customer customer;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
String ID = "xuefeng";
customer = Bean.Customer.getRecord(ID);
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<body>");
if(customer == null)
out.print("sdfsdf");
out.print("<h2>The information of the user:<h2><br>");
out.print("</body>");
out.print("</html>");
}
}
在与它同级的Bean(自己创建)文件夹里有Customer.class
但是上述代码返回的customer 为空
可是test.java
import Bean.*;
import java.sql.*;

public class test
{
public static void main(String[] args){
try
{
String ID = "xuefeng";
Customer customer = Customer.getRecord(ID);
System.out.println(customer.getFirstname() + " ");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
运行正常,是servlet需要额外配置什么东西么???
请大家赐教

搜索更多相关主题的帖子: bean  servlet  import  数据库  Bean  

----------------解决方案--------------------------------------------------------
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<body>");
if(customer == null)
out.print("sdfsdf");
out.print("<h2>The information of the user:<h2><br>");
out.print("</body>");
out.print("</html>");

光凭这些代码,我想知道你是怎样判断你的customer是空的

----------------解决方案--------------------------------------------------------
if(customer == null)
out.print("sdfsdf");
它输出了sdfsdf
而且如果强行输出所读取得值,会报出
java.lang.NullPointerException
ViewServlet.doGet(ViewServlet.java:22)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

----------------解决方案--------------------------------------------------------
哦,对,是我不小心没有看清楚,对不起

----------------解决方案--------------------------------------------------------
介意我看一下 Bean.Customer 这个类么?

----------------解决方案--------------------------------------------------------
customer = Bean.Customer.getRecord(ID);

这句话什么意思 为什么get方法带着参数啊


----------------解决方案--------------------------------------------------------
web.xml配置了么?
----------------解决方案--------------------------------------------------------

package Bean;
import java.sql.*;

public class Customer
{
private int CustomerId;
private String Firstname;
private String Lastname;
private String Address1;
private String Address2;
private String Tel;

public Customer()
{
CustomerId = 0;
Firstname = "";
Lastname = "";
Address1 = "";
Address2 = "";
Tel = "";
}

public Customer(int CustomerId, String Firstname, String Lastname, String Address1, String Address2, String Tel)
{
this.CustomerId = CustomerId;
this.Firstname = Firstname;
this.Lastname = Lastname;
this.Address1 = Address1;
this.Address2 = Address2;
this.Tel = Tel;
}

public static Customer getRecord( String Firstname )
{
try
{
Conn con = new Conn();
ResultSet rs = con.getRs("SELECT * FROM Customer where Firstname=\'" + Firstname + "\'");
if(rs.next())
{
Customer customer = new Customer(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
return customer;
}
}
catch(Exception e)
{
}
return null;
}

public String getFirstname()
{
return this.Firstname;
}

public String getLastname()
{
return this.Lastname;
}

public String getAddress1()
{
return this.Address1;
}

public String getAddress2()
{
return this.Address2;
}

public String getTel()
{
return this.Tel;
}
/*
public static void main(String[] args)
{
try
{
String ID = "xuefeng";
Customer customer = Customer.getRecord(ID);
System.out.println(customer.getFirstname() + " ");
System.out.println(customer.getLastname() + " ");
System.out.println(customer.getAddress1() + " ");
System.out.println(customer.getAddress2() + " ");
System.out.println(customer.getTel() + " ");
System.out.println(customer.getFirstname() + " ");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
*/
}


----------------解决方案--------------------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<!-- Define servlets that are included in the example application -->

<servlet>
<servlet-name>ViewServlet</servlet-name>
<servlet-class>ViewServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ViewServlet</servlet-name>
<url-pattern>/view</url-pattern>
</servlet-mapping>

<security-constraint>
<web-resource-collection>
<web-resource-name>Shopping</web-resource-name>
<url-pattern>/shop</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
</login-config>

<security-role>
<role-name>manager</role-name>
</security-role>
</web-app>
这个是xml,是不是还需要加点什么?


----------------解决方案--------------------------------------------------------
好象挺棘手的呢


----------------解决方案--------------------------------------------------------
  相关解决方案