关于servelet的一个问题,希望有人能帮我下

03-12-02 sunnaiti
我写了一个简单的程序,服务器用servelet,客户用html+javascript

要实现的功能是点击客户上的确定按扭,就会实现cookies会话,从而达到在服务器端计算某个用户的访问次数的功能。可是在部署完servelet,在运行的时候出现空指针异常,下面是我写的源程序,希望大家能帮帮我

客户端

名字:Login.html

<html>

<head>

</head>

<body bgcolor=pink>

<br>

<br>

<form method="post" action="http://127.0.0.1:8000/GiftServletContext/GiftServlet">

<table>

<tr>

<td>输入登录名:</td>

<td><input type="text" name="loginid"></td>

</tr>

<tr>

<td>输入密码:</td>

<td><input type="password" name="passwd"></td>

</tr>

</table>

<input type="submit" value="确定">

</form>

</body>

</html>

服务器端

名字:GiftServler.java

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

//import java.util.*;

//import java.sql.*;

public class GiftServlet extends HttpServlet

{

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

{

boolean cookieFound=false;

Cookie myCookie=null;

Cookie[] cookieset=req.getCookies();

res.setContentType("text/html");

PrintWriter pw=res.getWriter();

pw.println("<html>");

pw.println("<body>");

if(cookieset.length!=0)

{

for(int i=0;i<cookieset.length;i++)

{

if(cookieset.getName().equals("logincount"))

{

cookieFound=true;

myCookie=cookieset;

}

}

}

if(cookieFound)

{

int temp=Integer.parseInt(myCookie.getValue());

temp++;

if (temp==5)

pw.println("Congratulations!!!!!!!!!!!!!!!!!!!, a gift is awaiting you");

pw.println("The number of times you have logged in is: "+String.valueOf(temp));

myCookie.setValue(String.valueOf(temp));

int age=60*60*24*30;

myCookie.setMaxAge(age);

res.addCookie(myCookie);

cookieFound=false;

}

if(cookieFound==false)

{

int temp=1;

pw.println("This is the first time you have logged on to the server");

myCookie=new Cookie("logincount",String.valueOf(temp));

int age=60*60*24*30;

myCookie.setMaxAge(age);

res.addCookie(myCookie);

}

pw.println("</body>");

pw.println("</html>");

}

}

猜你喜欢