This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Execute a JSP page with client side Java. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Execute a JSP page with client side Java." Watch "Execute a JSP page with client side Java." New topic
Author

Execute a JSP page with client side Java.

Larry Nelsen
Greenhorn

Joined: Mar 14, 2011
Posts: 10
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?

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Larry Nelsen
Greenhorn

Joined: Mar 14, 2011
Posts: 10
"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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Mar 14, 2011
Posts: 10
"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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Mar 14, 2011
Posts: 10
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Mar 14, 2011
Posts: 10
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Larry Nelsen
Greenhorn

Joined: Mar 14, 2011
Posts: 10
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Mar 14, 2011
Posts: 10

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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

Joined: Mar 14, 2011
Posts: 10
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();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute a JSP page with client side Java.
 
Similar Threads
session issues with Ctrl-N
trying to submit form on another site from my JSP
How to pass input values to the JSP using POST method
Duplicate form submission - Synchronizer Token Pattern
problem with request forward