我在开发servlet applet通信程序时遇到了会话问题。
我成功登录用户后,在jsp中有登录页面我将用户引导到一个有applet的index.jsp页面。我在重定向到index.jsp之前在servlet中设置了一个会话变量。在我的applet中,我必须在会话中获取数据,并从数据库和属性文件中获取一些数据。我的代码是
//的Servlet
if(action.equals("login")){
uinfo = new UserInformation();
String uid = request.getParameter("txt_UserName");
String pwd = request.getParameter("pwd_PassWord");
int i = uinfo.authenticateUser(uid, pwd);
//System.out.println(i);
if(i==1){
session.setAttribute("uName", uid);
request.getRequestDispatcher("index.jsp").forward(request, response);
else if(action.equals("applet")){
try{
output = new ObjectOutputStream(response.getOutputStream());
uinfob= new UserInformationBean();
Properties prop = new Properties();
try {
prop.load(new FileInputStream(path+"/connection.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
uinfob.setHost(prop.getProperty("host"));
uinfob.setIs_Active("Y");
uinfob.setUid(session.getAttribute("uName").toString());
output.writeObject(uinfob);
output.flush();
//output.close();
}catch(IOException iox){
iox.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
output.close();
}
}
在我的小程序中
public void init() {
try{
String queryString="action=applet";
URL codeBase = getCodeBase();
URL servletURL = new URL(codeBase.getProtocol(), codeBase.getHost(), codeBase.getPort(), "/SwingBrowser1/UserServlet?"+queryString);
servletConnection = servletURL.openConnection();
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setRequestProperty("Content-Type","application/octet-stream");
getData();
}catch(Exception e){
e.printStackTrace();
}
}
// TODO overwrite start(), stop() and destroy() methods
public void getData()
{
try{
ObjectInputStream input = new ObjectInputStream(servletConnection.getInputStream());
UserInformationBean uib =(UserInformationBean)input.readObject();
System.out.println(uib.getHost()+"-------------"+uib.getIs_Active()+"-----------------"+uib.getUid());
input.close();
}
catch( Exception e)
{
e.printStackTrace();
}
它正在获取java.io.EOFException错误,我在我的else servlet块中使用了sop来测试它。我之前设置的属性是返回空值。