我要实现的是一个在服务器端对客户端的http请求的响应的servlet
源程序如下:
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
java.util.Enumeration headers = request.getHeaderNames();
System.out.println("HTTP头信息");
while(headers.hasMoreElements()){
String header = (String)headers.nextElement();
System.out.println(header+"="+request.getHeader(header));
}
String userName = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(userName+":"+password);
DataOutputStream dos = new DataOutputStream(response.getOutputStream());
if(userName.equals("ming") && password.equals("pass")){
dos.writeInt(100);
}else{
dos.writeInt(200);
}
dos.flush();
dos.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
其实就是一个用户登陆的功能..如果用户名密码正确返回100,错误返回200
而我在测试的时候客户端返回的是***********
于是我用ie打开...报错如下
报错如下:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
LoginServlet.processRequest(LoginServlet.java:22)
LoginServlet.doGet(LoginServlet.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
我是新手..可能烦些幼稚的错误..希望大家给点详细的解释..谢谢了.
參考答案:看你的系统报错是空指针异常,而系统中自己维护的业务代码
也就是这行
f(userName.equals("ming") && password.equals("pass")){ 。。}
你应该写成"ming".equals(userName) && "pass".equals("password") ;可能你的东西没传过来