This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes Problem with passing object from servlet to applet using jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with passing object from servlet to applet using jsp page" Watch "Problem with passing object from servlet to applet using jsp page" New topic
Author

Problem with passing object from servlet to applet using jsp page

Walter Krawiec
Greenhorn

Joined: Jan 13, 2009
Posts: 20
Hi there,

I've problem with sending own object using servlet output stream, after compile web application I'm pressing button (part of JSP page) and popup comes out with object data that suppose to be sent to applet, how to solve this problem?


button that invokes method(JSP page):

public String bopenApplet_action() {
try {
CalendarView[] calView = new HibernateMngr().loadCalendarData();

HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
session.setAttribute("sql", calView);
return "toApplet";

} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}

// toApplet is redirection to servlet Page


Servlet code:



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package engine.applet;

import engine.Calendar.CalendarView;
import engine.HibernateMngr;
import engine.tables.TaskCalendar;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SqlServlet extends HttpServlet {

public void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

HttpSession session = request.getSession();

execute(session, request, response);

}

public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}

protected void execute(
HttpSession ses,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {


CalendarView cv[] = (CalendarView[]) ses.getAttribute("sql");
System.out.println("name: " + cv[0].getTask().getName());


response.setContentType("application/x-java-serialized-object");

ObjectOutputStream outputToApplet;
outputToApplet = new ObjectOutputStream(response.getOutputStream());

outputToApplet.writeObject(cv[0]);

outputToApplet.flush();
outputToApplet.close();
System.out.println("Data transmission complete.");
}
}



and applet code:


public void run() {
// HttpSession session =
while (true) {
try {
loadObj();
Thread.sleep(100);

} catch (InterruptedException ex) {
Logger.getLogger(applet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}


private void loadObj() {
try {
// connect to the servlet
URL DBservlet = new URL("http://localhost:8080/HibernateTest/TaskCalendar.jsp");
URLConnection servletConnection = DBservlet.openConnection();

servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
servletConnection.setRequestProperty("Content-Type","application/x-java-serialized-object");


ObjectInputStream inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());

CalendarView cv = (CalendarView) inputFromServlet.readObject();

servletConnection.getInputStream().close();
inputFromServlet.close();

if (cv != null) {
// printData(cv);
System.out.println("cv is not empty!");
}
else{
System.out.println("cv is empty!");

}

} catch (IOException ex) {
Logger.getLogger(applet.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(applet.class.getName()).log(Level.SEVERE, null, ex);
}
}


I don't understand why this data just popup's.

Anyone know answer?


cheers




 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with passing object from servlet to applet using jsp page