This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Execute a JSP page with client side Java.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I would like to execute a JSP page from a java client without a browser.

Sample.jsp
[begin code]-----------------------------------------------------------------------------
<%
. . .
// login
PassportLogon pl = new PassportLogon();
if (pl.isValidUser(userId, password, host, port, opsys) != true) { }
else
{
session.setAttribute("ConnectionEstablished", sConnectionInd);
%>


<html>
<body>
<form name="XXXXX" action="servlet/PollerMaintenance" method="POST">
Region: <%= request.getParameter("ppregion") %>
Touchpoint: <%= request.getParameter("TouchpointId") %>
Action: <%= request.getParameter("Action") %>
<input type="hidden" name="TouchpointId" value=<%= request.getParameter("TouchpointId") %> >
<input type="hidden" name="Action" value=<%= request.getParameter("Action") %> >
</form>
<script language="JavaScript" type="text/javascript">
document.XXXXX.submit();
</script>
</body>
</html>

<% } %>
<% } %>
[end code]-----------------------------------------------------------------------------



java client
[begin code]-----------------------------------------------------------------------------
. . .
String strURL = "http://" + Server_in + ":" + Port_in + "Sample.jsp" + params ;
URL myURL = new URL(strURL);
HttpURLConnection huc = (HttpURLConnection) myURL.openConnection();
huc.setRequestMethod("POST");

huc.connect();
OutputStream rawOutStream = huc.getOutputStream(); // I get the HTML page. BUT... FORM is NOT submitted automatically with the "document.WOTASK.submit();"
[end code]-----------------------------------------------------------------------------



Comments
What I can do:
I can connect ang get HTML page. BUT... FORM is NOT submitted automatically with the "document.WOTASK.submit();"
I can call Sample.jsp from my firefox web browser and the JSP page connects and submits page.
I can create a vbscript which instantiates a web browser class and the JSP page connects and submits page.

What I can not do:
Create a Java program which runs on a UNIX server that can connect to web server and call a JSP page which performs java scripting code.
No Browser application.

Is there a Java class which can be used to call&execute a JSP page within JAVA?

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP creates HTLM, which requires something to interpret and display it. What are you intending to use instead of a browser? If there is nothing to display, would it not be better to just use RMI or something?
 
Larry Nelsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"JSP creates HTLM, which requires something to interpret and display it. What are you intending to use instead of a browser? If there is nothing to display, would it not be better to just use RMI or something? "

Paul.
There is nothing to display. Daily a user logs on to a web server, navigates to a page and hits the submit button. I have created a jsp page which logson with javascripting, populates a couple fields, and then autmatically submits. The JSP page works great in my browser. The JSP page works great in VBScript if I instantiate a browser and call the JSP Page. But.. I need to run this on a UNIX server with no browser.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That I understand, what I don't understand is why you have chosen to use GUI and client side technologies when neither are needed. Is there something I'm missing? If not, and if you ultimately are looking for an HTTP request with some parameters already set, you would probably find it much easier just submitting an HTTP request directly from Java. Have a look at the java.net package.
 
Larry Nelsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"why you have chosen to use GUI and client side technologies when neither are needed."

Paul, I am sure you are correct. I don't know how to call a servlet. I can view an existing JSP page and modify the code to automatically submit the form. I am using GUI and client side technologies because they provided a successful starting point. I am not familiar with RMI but it doesn't appear that I can call a servlet.

"submitting an HTTP request directly from Java. Have a look at the java.net package. "


I will look at the java.net.package again. Maybe the ContentHandler
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My point is you don't need JavaScript and you don't need HTML from the sound of things, so it does appear you are making work for yourself. Do you need to do anything with the response from this Servlet call?
 
Larry Nelsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

The servlet processes records in a queue. There are four types of records in the queue. I need to pass a parameter to the servlet identifying the type of record to process. A return code would be nice. Currently i query a database to determine number of rows unprocessed. Zero rows means success.

I imagine the best way is to call the servlet directly with params. But I believe the servlet references request html fields for parameter values. The servlet is vendor supplied and I don't believe I have source code.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Servlet will not request HTML fields, because HTML doesn't come into play where the actual request is concerned (HTML is just a GUI technology really). Ultimately it must pass specific parameters in the request (because that's how HTTP works), and these should be discoverable by looking at the source generated when you access it via a browser. I'd maybe use the UI and watch what params are passed with a tool like Fiddler (if its not immediately obvious from looking at the source) and see if you can replicate in a simple class. In theory this should be much simpler than having to invoke a browser instance on the fly.
 
Larry Nelsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK
What class or classes should I use to call a servlet


This is basically my GUI method.
<form name="theForm" action="servlet/PollerMaintenance" method="POST">
<input type="hidden" name="Parameter1" value="Value1" >
<input type="hidden" name="Parameter2" value="Value2" >
document.theForm.submit();

My vendor won't help.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the classes in java.net (such as URL and URLConnection), or a higher-level library such as HttpClient.

By the way, in case anyone gets confused by the topic title, there is no client-side Java involved with JSP and Servlets. All Java executes on the server.
 
Larry Nelsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the classes in java.net (such as URL and URLConnection), or a higher-level library such as HttpClient


I currently use URL and URLConnection to request my JSP page. I get my page with the output stream. How can I submit the page that is returned? Do I need a request object with a Request.submit?
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point is that you don't need a JSP at all. Just submit a request that hits the servlet with the appropriate information. You are creating a Rube Goldberg machine to do something that is actually quite simple.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Apache HTTP Components project has all you need to emulate a browser conversation with a web server as a Java client. People do it all the time.

Bill
 
Larry Nelsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do you call a servlet with HTTPConnection?

I can connect with
URL myURL = new URL("http:\\Server:8080\whatever\whatever.jsp);
HttpURLConnection con = (HttpURLConnection) myURL.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.connect();
// con.getResponseCode() returns 200 OK

How can it I con.submit("servlet/PollerMaintenance&Parameter1="value1"&Parameter2="Value2);
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stop with the JSP! No JSP. None.

Instead of the URL of the JSP, just hit the URL of the servlet. I'm not sure why you have it so stuck in your mind that a JSP must be used to contact a servlet. It's just addressed by URL in the same way that a JSP, or HTML or anything else is addressed.
 
Larry Nelsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
URL url = new URL("http://192.168.1.101:8080/servlet/PollerMaintenance");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);

BufferedWriter out =
new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) );
out.write("parameter1=value1¶meter2=value2");
out.flush();
out.close();
BufferedReader in =
new BufferedReader( new InputStreamReader( conn.getInputStream() ) );

String response;
while ( (response = in.readLine()) != null ) {
System.out.println( response );
}
in.close();
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic