dcsimg
Servlet request by java code
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Perica_Markovic
Posted On:   Friday, October 10, 2003 08:07 AM

Hello,

I want to send a request to a servlet by java code.
I have a session bean and I want to send a request by url to a servlet
Could somebody help me?

Thanx

Perica Markovic

Re: Servlet request by java code

Posted By:   s_kandacharam  
Posted On:   Friday, October 10, 2003 12:19 PM

If you are trying to access servlet from java client program, you should do that using URL objects.On the client side, java program should use the following code to call servlet


String servletGET = "http://perica:80/webApp/MyServlet?param1="+URLEncoder.encode("Give Response");

URL urlServlet = new URL( servletGET );

URLConnection servletConnection = urlServlet.openConnection();

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

Vector = (Vector) theInputFromServlet.readObject();


On the sevlet side, you can access user request and send the response like the following


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


String strUserRequest = request.getParameter("param1");

if(strUserRequest.equals("Give Response")){

Vector vData = myDataAccessor.getDataList();

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

resOutPut.writeObject(vData);

resOutPut.flush();

resOutPut.close();

}


}


Hope this helps you.

Kandacharam
About | Sitemap | Contact