Solved

Sending a Java Object to JSP page

Posted on 2004-04-28
4
611 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rebasing and merging used in SCM 1 78
Unhandled exception type Exception 18 42
jsp login check 12 42
mysql jsp example issue 32 48
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

790 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