Solved

Sending a Java Object to JSP page

Posted on 2004-04-28
4
605 Views
Last Modified: 2010-05-18
Hello fellow experts,

Hi I want to send a Java String to a jsp page and I want the jsp page to send a vector back to the java program.

half is done...I think...

in my java class
URL url = new URL(strPath);
         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
         conn.setDoOutput(true);
         conn.setRequestMethod("POST");
         conn.setRequestProperty("Content-type", "application/octet-stream");
         OutputStream os = conn.getOutputStream();
         ObjectOutputStream out = new ObjectOutputStream(os);
         
     
         out.writeObject(strXMLParams);
         out.flush();
         out.close();

I just need the code to receive and then to send back an object to the java the java code to receive the vector would be nice too
thanks
Ghost
0
Comment
Question by:copyPasteGhost
  • 2
4 Comments
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 10943104
this question is seeming to overlap into this question

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20968074.html

anyone know what to do?
Ghost
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10945439
Have you tried request.setAttribute() and request.getAttribute()??? they shd be able to get your job done..!!
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10945483
Or if you are adamant doing it above described way then

in your jsp.
ObjectInputStream oip = new ObjectInputStream(request.getInputStream());
YourObj obj = (YourObj)(oip.readObject());


This stuff gonna throw a lot of exceptions so a try catch block is on the card obviously..:-)
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 10945588
I am not sure if I understand your requirements. But I did write the following two pages(located in same directory).      rrz  

------------sender.jsp----------------------------------------
<%@ page import="java.util.*,java.net.*,java.io.*" %>
<%  
         Vector vSend = new Vector();
         vSend.add("one");
         vSend.add("two");
         vSend.add("three");
         String urlStr = request.getRequestURL().toString();
         URL url = new URL(urlStr.substring(0,urlStr.lastIndexOf("/")+1) + "bouncer.jsp");
         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
         conn.setDoOutput(true);
         conn.setDoInput(true);
         conn.setRequestMethod("POST");
         conn.setRequestProperty("Content-type", "application/octet-stream");
         ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());
         oos.writeObject(vSend);
         oos.flush();
         ObjectInputStream in = new ObjectInputStream(conn.getInputStream());
         Vector vBack = (Vector)in.readObject();
         in.close();
         oos.close();
%>
vBack=<%=vBack%>

-------------------------------bouncer.jsp---------------------------------------------
<%@ page import="java.net.*,java.io.*" %>
<%
         ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
         ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
         oos.writeObject(ois.readObject());
         oos.flush();
         ois.close();
         oos.close();
%>
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now