我在确定是否可以在不接受HttpServletRequest和HttpServletResponse参数的方法中实际调用会话bean的方法时遇到了一些问题?
我意识到代码在doGet和doPost方法中有效,我修改了一个自定义方法来获取请求和响应的参数,然后它突然开始工作了。
我的目标是让方法工作(调用会话bean方法)而不在方法参数中有请求和响应。
然而,这是有问题的版本的代码,它在itemRegistrationSession.openConnection()上授予NullPointerException;
package web;
import java.io.IOException;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import auction.itemRegistrationRemote;
import auction.userRegistrationRemote;
/**
* Servlet implementation class processItem
*/
@WebServlet("/processItem")
public class processItem extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB itemRegistrationRemote itemRegistrationSession;
@EJB userRegistrationRemote processRegistrationSession;
/**
* @see HttpServlet#HttpServlet()
*/
public processItem() {
super();
getCategories();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//ArrayList<String> categories = getCategories(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected ArrayList<String> getCategories() {
System.out.println("Hello from getCategories()");
itemRegistrationSession.openConnection();
System.out.println("Hello2 from getCategories()");
ArrayList<String> categories = itemRegistrationSession.getCategories();
itemRegistrationSession.closeConnection();
return categories;
}
}
package auction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
/**
* Session Bean implementation class itemRegistration
*/
@Singleton
public class itemRegistration implements itemRegistrationRemote {
Connection con;
PreparedStatement prepStat;
ResultSet resultSet;
final String ITEM_NAME = "ITEM_NAME";
final String CATEGORY_NAME = "CATEGORY_NAME";
final String USER_NAME = "USER_NAME";
final String ITEM_MODEL = "ITEM_MODEL";
final String ITEM_DESCRIPTION = "ITEM_DESCRIPTION";
/**
* Default constructor.
*/
public itemRegistration() {
// TODO Auto-generated constructor stub
}
@Override
public void openConnection() {
System.out.println("Openning connection for itemRegistration");
try {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver).newInstance();
} catch (InstantiationException e) {
System.out.println("Openning database connection - Instantiation Exception " + e.getMessage());
} catch (IllegalAccessException e) {
System.out.println("Openning database connection - IllegalAccessException " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("Openning database connection - ClassNotFoundException " + e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:derby:C:/Users/Adrian/MyDB;upgrade=true", "adrian", "testero");
} catch (SQLException e) {
System.out.println("Openning database connection - SQLException " + e.getMessage());
}
} // End of openConnection method
@Override
public void closeConnection() {
try {
resultSet.close();
prepStat.close();
con.close();
} catch (SQLException e) {
System.out.println("Closing database connection - SQLException " + e.getMessage());
}
} // End of closeConnection method
@Override
public ArrayList<String> getCategories() {
ArrayList<String> categories = new ArrayList<String>();
try {
prepStat = con.prepareStatement("SELECT * FROM CATEGORY");
resultSet = prepStat.executeQuery();
int i = 1;
while (resultSet.next()) {
categories.add(resultSet.getString(i));
i++;
}
} catch (SQLException e) {
System.out.println("getCategories SQLException - " + e.getMessage());
}
return categories;
}
}
答案 0 :(得分:1)
听起来像
的依赖注入@EJB itemRegistrationRemote itemRegistrationSession
无效。尝试添加一个显示itemRegistrationSession对象值的日志语句,以查看该成员是否已正确初始化。
答案 1 :(得分:0)
您正在获得异常,因为当您调用getCategories()
时尚未注入EJB。
在构造函数调用时,还没有发生注入,请记住刚刚创建对象,因此容器没有时间注入任何东西,正确的方法是使用{{3}注释。来自javadoc:
PostConstruct注释用于在完成依赖注入以执行任何初始化之后需要执行的方法...
因此您需要将getCategories()
调用移动到@PostConstruct注释方法,如下所示:
@WebServlet("/processItem")
public class processItem extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB itemRegistrationRemote itemRegistrationSession;
@EJB userRegistrationRemote processRegistrationSession;
/**
* @see HttpServlet#HttpServlet()
*/
public processItem() {
super();
}
@Postconstruct
void init() {
getCategories();
}...