-2

我已经创建了一个 msacess db mydb.accdb并将 odbc 源指向它,我在 lserv.java 文件中有以下代码,所有必要的 tomcat 配置都已完成,servlet 没有响应,所以我添加了一个打印错误显示以下错误的 init 方法

javax.servlet.ServletException: Servlet.init() for servlet login threw exception
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
    org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
    java.lang.Thread.run(Thread.java:722)
root cause

java.lang.NullPointerException
    lserv.init(lserv.java:23)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
    org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
    java.lang.Thread.run(Thread.java:722)

java代码

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class lserv extends HttpServlet
{
Connection con;
ResultSet rs;
Statement st;
PrintWriter out;
public void init() 
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydb");
st=con.createStatement();

}
catch(Exception e)
{
out.println(e.getMessage());
}
}

public void doGet(HttpServletRequest req,HttpServletResponse res) 
{

try
{
out=res.getWriter();
res.setContentType("text/html");
if(req.getParameter("sub").equals("value"))
{
String s1=req.getParameter("t1");
String s2=req.getParameter("t2");
rs=st.executeQuery("SELECT * FROM login WHERE uname='"+s1.trim()+"AND pass='"+s2.trim()+"';" );
if(rs.next())
{
out.println("welcome");
}
else
{
out.println("sorry");
}

}
}
catch(Exception e){
out.println(e.getMessage());
}
}
}

html页面代码

<html>
<title>Login System</title>
<style type="text/css">
div.ex {
width:220px;
padding:10px;
border:5px solid gray;
margin:10px;
}
</style>

<body>
<div class="ex">
<strong>User Login</strong>
<form name=f1 method=get action="http://localhost:8080/login">
User &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type=text name=t1><br>
Password <input type=password name=t2><br>
<input type=submit name=sub value="Login">
</div>

</body>
</html>

我有一个名为 login 的表,其中包含字段 uname 并传递值。

什么问题?请帮忙

4

1 回答 1

0

我认为这应该很清楚;

java.lang.NullPointerException
    lserv.init(lserv.java:23)
    [...]

只需修复代码第 23 行中的 NPE。:)

对我来说,这条线似乎导致了错误:

out.println(e.getMessage());

该变量out未初始化,因此null.

于 2013-02-23T19:11:22.680 回答