各位前辈,帮忙一下
weblogic的Servlet创建之后,报空指针异常
但是找不出原因
所有代码如下:【只有一个servlet,且数据源配置正常,实验过了的,web.xml也配置完成】
package myservlet;
import java.awt.Container;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.StatementImpl;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
public class StudentServlet extends HttpServlet {
public StudentServlet(){
super();
}
public void destroy() {
super.destroy();
}
private static final String CONTENT_TYPE="text/html;charset=GBK";
//Do Get 方法
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//打印网页
out.println("<HTML>");
out.println("<HEAD><TITLE>StudentServlet</TITLE></HEAD>");
out.println("<BODY bgcolor=\"#fffff\">");
if ((request.getParameter("sno"))==null ||(request.getParameter("sno")).equals("input number")){
out.println(
"<form method='post' action='/lab05-2/src/myservlet/StudentServlet'>");
//"<form method='post' action='/lab05-2/StudentServlet'>");
out.println("");
out.println(
"学号查询:<input type='text' name='sno' vaule='input number'/>");
out.println("<input type ='submit' vaule='查询'/><br>");
out.println("</form>");
}
//另外一种情况
else if(!(request.getParameter("sno")).equals("input number")){
Vector vc =new Vector();
out.println("<Form method='post' action='/lab05-2/src/myservlet/StudentServlet'>");
out.println("");
out.println("学生学号:<input type='text' name='sno' />");
out.println("<input type ='submit' vaule='查询'/><br>");
out.println("</form>");
out.println("<hr>");
out.println("<table border='1'><tr>");
out.println("<th>学号</th><th>姓名</th><th>语文</th><th>数学</th></tr>");
try{
if((vc=accessDB(Integer.parseInt((String)request.getParameter("sno"))))!=null){
out.println("<tr>");
out.println("<td>"+vc.elementAt(0)+"</td>");
out.println("<td>"+vc.elementAt(1)+"</td>");
out.println("<td>"+vc.elementAt(2)+"</td>");
out.println("<td>"+vc.elementAt(3)+"</td>");
out.println("</tr>");
}
}catch(Exception e) {
out.println("<tr>");
out.println("<td>没有记录</td>");
out.println("</tr>");
out.println("<td>数据库里没有你要查询的记录</td>");
e.printStackTrace();
}
out.println("</table>");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
//Do Post方法
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Init方法
public void init() throws ServletException {
}
//通过weblogic的JNDI服务来查询数据库数据
public Vector accessDB(int id){
Vector vc=new Vector();
Context ctx;
javax.sql.DataSource ds = null;
java.sql.Connection myConn = null ;
final String JNDI_DATABASE_NAME="AA";
Properties ht =new Properties();
//Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try{
ctx = new InitialContext(ht);
ds = (javax.sql.DataSource)ctx.lookup(JNDI_DATABASE_NAME);
}catch(Exception e){
e.printStackTrace();
}
if(ds == null){
System.out.println("Eorror !!!");
}
else{
System.out.println("Connection is OK !!!");
}
java.sql.PreparedStatement myStatement = null;
ResultSet mySet = null;
try{
myConn=ds.getConnection();
myStatement=myConn.prepareStatement("select * from stdb where sno=?");
myStatement.setInt(1, id);
mySet=myStatement.executeQuery();
while (mySet.next()){
vc.add(Integer.toString(mySet.getInt("sno")));
vc.add(mySet.getString("sname"));
vc.add(Integer.toString(mySet.getInt("chinese")));
vc.add(Integer.toString(mySet.getInt("math")));
}
//关闭
myStatement.close();
mySet.close();
myConn.close();
return vc;//返回vc
}catch(Exception e) {
e.printStackTrace();
}
return vc;
}
public static void main(String[] args) {
StudentServlet aa=new StudentServlet();
System.out.println(aa.accessDB(100).elementAt(1));
}
}
【aa和student只是测试着用,与StudentServlet无关】
报错为:
大概就是这些,请指教,
初学,找过百度等也没有解决