?
Solved

Sending a Java Object to JSP page

Posted on 2004-04-28
4
Medium Priority
?
615 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 28

Accepted Solution

by:
rrz earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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