Servlet实现Basic网页认证,出错,请问如何解决?(程序编译成功!)(50分)

  • 主题发起人 主题发起人 Java小子
  • 开始时间 开始时间
J

Java小子

Unregistered / Unconfirmed
GUEST, unregistred user!
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import sun.misc.BASE64Decoder;
public class Login extends HttpServlet
{
private String validUsername="liu";
private String validPasswd="i";
public voiddo
Get(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String auth=request.getHeader("Authorization");
if(auth==null)
{
chall(response);
}

else

{
if(!auth.toLowerCase().startsWith("basic"))
chall(response);
}


String namess=auth.substring(6).trim();
BASE64Decoder decode=new BASE64Decoder();
namess=new String(decode.decodeBuffer(namess));
int c=namess.indexOf(":");
String username=namess.substring(0,c);
String password=namess.substring(c+1);
if(!username.equals(validUsername)||!password.equals(validPasswd))
chall(response);
else
{
PrintWriter outt=response.getWriter();
outt.println("ok");}

}
private void chall(HttpServletResponse response)
{
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authen
ticate","Basic realm=/""+"java/"");
}

}
Error: 500
Location: /myjsp/servlet/Login
Internal Servlet Error:
java.lang.NullPointerException
at Login.doGet(Login.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:536)
 
空指针错误。
附上我写的例子:http://www.delphibbs.com/keylife/iblog_show.asp?xid=913
 
后退
顶部