• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Auto-submit a form on a jsp page

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a jsp form. The requirement is that it gets all the details from a servlet and displays on the jsp form page. This has to be done for more than 100 users and it should be automatic. ALso all the information displayed in the text boxes should be submitted automatically to a servlet without any user intervension. How can this be done.
Actually I have got a jsp form to get the details for 10 users (for example) and I use meta-tag refresh and display the details.
The next part is I have the script
<script language="JavaScript" type="text/javascript">
//document.frmServeBetter.action = /test/ctrl?do=SUBMIT";// This should go to a servlet
document.frmServeBetter.submit();
</script>
at the bottom of the page, ie after the </form> tag.


But how can I integrate this?

But even when I dont have the meta tag refresh, all the 10 users are displayed and the loop wont stop. Please can you go thru my jsp code and let me know where I am wrong.
Thanks for the help.



JSP FILE
---------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Information</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<LINK href="/test/images/survey_StyleSheet.css" type=text/css rel=stylesheet>
<META content="Microsoft FrontPage 5.0" name=GENERATOR>
<!-- <meta http-equiv="refresh" content="5"> -->

<%@page import="java.lang.*"%>
<%@page import="java.util.*" %>

<% String strCaller = new String(request.getRemoteHost());%>
<script>
function fnSubmit()
{
alert("Inside fnSubmit: ");

document.frmServeBetter.action = "/test/ctrl?do=SUBMIT";

document.frmServeBetter.submit();

}

</script>

</HEAD>

<BODY>
<form name="frmServeBetter" method="post" >

<%

HashMap objHashMap = new HashMap();
ArrayList objArrayList = new ArrayList();

//objHashMap = (HashMap)request.getAttribute("DETAILS");

objArrayList = (ArrayList)request.getAttribute("DETAILS");

%>

<%!
static int size = 10;
static int counter = 0;
static int counter1 = 0;
%>

<INPUT TYPE=hidden Name="Caller" value=strCaller>


<TABLE class=content borderColor=#FEE25B cellSpacing=0 cellPadding=0 width=700
border=1>
<TBODY>


<TR>

<TD vAlign=top align=middle height=353>
<table width="650" border="0" cellspacing="0" cellpadding="5">


<tr>
<td height="351" align="center" valign="top" bgcolor="#FFFced">
<TABLE cellSpacing=0 cellPadding=3 width=700 border=0>
<TBODY>
<TR>
<TD colspan="2" class=content><img src="/test/images/space.gif" width="5" height="8"></TD>
</TR>

<TR class=content>
<TD height=14 colspan="2"><img src="/test/images/space.gif" width="5" height="8"></TD>
</TR>

<%
while(counter < size)
{
objHashMap = (HashMap)objArrayList.get(counter);
%>
<TR>
<TD class=content height=25><font color="#FF0000">FirstName :</TD>


<TD> <INPUT class=field value = "<%=objHashMap.get("FIRSTNAME")%>" name="FirstName" size="20" ></TD>

<TR>
<TD class=content height=25><font color="#FF0000">LastName :</TD>
<TD> <INPUT class=field value = "<%=objHashMap.get("LASTNAME")%>" name="LastName" size="20"></TD>
</TR>


<TR>
<TD colspan="2"><img src="/test/images/space.gif" width="5" height="8"></TD>
</TR>
<TR>
<TD class=content><font color="#FF0000">EMail Address :</TD>
<TD> <INPUT class=field value = "<%=objHashMap.get("EMAIL")%>" name="EMailAddress" size="20"></TD>
</TR>

<%

counter ++;
%>

<%
break;
}//whileloop
%>
</TR>


<TR>

<TD class=content height=24><INPUT class="btn" type="Reset" value=Cancel name="B1"></TD>
</TR>
<TR>
<TD class=content colSpan=2 height=24> </TD>
</TR>
</TBODY>
</TABLE>
</td>
</tr>
</table></TD></TR></TBODY></TABLE>
<p> </p>

</form>
<script language="JavaScript" type="text/javascript">
//document.frmServeBetter.action = "/test/jsp/test1.jsp";
document.frmServeBetter.submit();
</script>


</BODY>
</HTML>

---------------------------------------------------------------

Thanks once again. If you didnt understand any part, please let me know.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you a little confused ;
You are all wrong

So... Where to begin...

1. do not use META refresh at all in your case. althougth it may be helpful, but not in your case, because form must be submitted and don't redirected
if I understand your problem clearly.

2. You want to display one user info at a time and then
automaticaly submit it after a while? Then you have misconseption about
what is going on on server and what's going on in browser.

So... In servlet save details in session
I do not know way in which you organize you servlets,
but I think you'll be able use those snippets.
####################################
Servlet #1 lets map it something like /showAll
//.... in body of servise method
List objArrayList = get reference to info list
//assert objArrayList.size() > 0 : "Non Empty List";
request.getSession().setAttribute ("DETAILS", objArrayList);

Integer i = new Integer(1);
request.setAttribute("currentindex", i);

forward ("viewInfo.jsp") //use request dispatcher
//end of snippet
####################################
Servlet #2 let map it something like /oneInfo
//.... in body of servise method

//.... process submitted values from Info (HashMap) input field here

String si = request.getParameter("index");
int ii = safely convert si to int.....
List l = (List)request.getSession().getAttribute("DETAILS");
if (l.size() >= ii) {
request.getSession().removeAttribute("DETAILS");//CleanUp
forward ("end_of_loop") //use request dispatcher to go out of this loop
}
request.setAttribute("currentindex", new Integer(ii + 1));
forward ("viewInfo.jsp") //use request dispatcher to display next
//end of snippet

.....
####################################
And finally JSP
.....
<%
List objArrayList = (List)request.getAttribute("DETAILS");
Integer currenIndex = request.getAttribute("currentindex");
HashMap m = (HashMap)objArrayList.getAt(currenIndex.intValue());
%>
<form name="frmServeBetter" method="post" action="/oneInfo" ><!--Servlet#2 will process this form -->
...............
<INPUT class=field value = "<%=objHashMap.get("FIRSTNAME")%>" name="FirstName" size="20" >..........
<INPUT class=field value = "<%=objHashMap.get("LASTNAME")%>" name="LastName" size="20">..........
<!--AND SO ON WITH ALL FIELDS OF RECORD-->
................
<!--Here some inportant things. Hidden Index that we use in servlet#-->
<input type="hidden" name="index">
<input type="Submit" style="display:none"/>
</form>

<script type="text/javascript">
window.on1oad = function () {
var act = function(){document.frmServeBetter.submit()};
window.setTimeout(act, 5000 /* timeout in miliseconds */);
}
</script>

Hope you'll understand main idea
[ March 14, 2005: Message edited by: Eugene Lucash ]
 
Sheriff
Posts: 67752
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
Moved to HTML/Javascript.
 
Eugene Lucash
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear, Bear Bibeault.
I think you have made a mistake.
this is more JSP question, about how leverage flow of server
side iteration, so little script acts like helper and not more.
Consider reading & understanding posts before moving something..

TAB,F6,ENTER,TAB,F6,ENTER.....
... I like to move it! move it!,I like to move it! move it!
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Eugene,
Thanks for your reply. Also I am not sure where to post this message now, apologies if I put it in wrong place.

Yes, I was very much confused, since I didnt have any proper requirement, moreover this was very new. This was also the basic draft of my code. Thanks for pointing me out.

Actually I will not be able to work on what you told for now as I am told to do this in a different way, but sure I will do this at my house whenever time permits.

Now what my people said to me is that, they want to retrieve all the details from our databse and submit it to another website and submit that data to the other site.

I will explain my work, so no one thinks I am submitting personal information or giving out details.

We are a travel agent software developer, we ourselves are a agent. So whatever data the user submits to us, we need that data to be submitted to our parent company or the agency. Like if its airtickets, we collect all details and submit the details to Delta, Continental, AA or other airline site. Likewise in cruises also. Hope you get what I am saying.

So now I am retreiving all the data, our customers had entered in our form from the database. But how can I get all the data to be submitted to other site. The exact details ahould be given.
Suppose if I get the first name, the exact data in the first name column should be submitted in the first name column of the other site. Am I making any sense.
Can this be done. I have no idea how to start. Any help would be appreciated.
Thanks
 
Eugene Lucash
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vjy Chin,
Althought you described your problem more cleanly now, still there are many
white spots.
I'll try with all info I got up to now, so...

You can avoid submitting forms in such explicit way.

First you must investigate interfaces of Other's site form in detail.
Pretend yourself like a spider bot surfing to company's website.
You don't see presentational markup,
you only see form, it's action url, http method (post or get).
And names of fields,
knowing their requirements to corect format of values

When you know this information,
you can use, for example, Apache commons HttpClient library
http://jakarta.apache.org/commons/httpclient/

With this library you can automatically construct REQUEST's
to appropriate server and fill them with appropriate values, then submit.

HttpClient used programmaticaly, for example in any servlet
or java application.


PS, Dear, Bear Bibeault, move this back to Servelts or JSP so more people
can put their thougth on this topic.
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks dude,

I am now able to get the form code now. The application is

---------------------------------------
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.HttpConnection;


public class SubmitHttpForm {

//private static String url = "http://localhost:8080/validatorStrutsApp/userInfo.do";

private static String url = "http://localhost:8080/test/jsp/test.jsp";

public static void main(String[] args) {



//Instantiate an HttpClient
HttpClient client = new HttpClient();

//Instantiate a GET HTTP method
HttpMethod method = new GetMethod(url);






//Define name-value pairs to set into the QueryString
NameValuePair nvp1= new NameValuePair("FirstName","fname");
NameValuePair nvp2= new NameValuePair("LastName","lname");


method.setQueryString(new NameValuePair[]{nvp1,nvp2});

try{
int statusCode = client.executeMethod(method);

System.out.println("QueryString>>> "+method.getQueryString());
System.out.println("Status Text>>>"
+HttpStatus.getStatusText(statusCode));

//Get data as a String
System.out.println(method.getResponseBodyAsString());

//OR as a byte array
byte [] res = method.getResponseBody();

//write to file
FileOutputStream fos= new FileOutputStream("donepage.html");
fos.write(res);

//release connection
method.releaseConnection();
}
catch(IOException e) {
e.printStackTrace();
}
}
}

----------------------------------------------------------


----------------------
But what I want is to get those values from
NameValuePair nvp1= new NameValuePair("FirstName","fname");
NameValuePair nvp2= new NameValuePair("LastName","lname");

to be shown in my jsp.

Now I am only able to view the jsp page in my command window and the .html page.

How should I go about it.
Thanks for your time and help.
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I have the values in the jsp. The Code I used is

----------------------------------------------
import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.*;

/**
* & lt;p>
* A example that demonstrates how HttpClient APIs can be used to perform
* form-based logon.
* & lt;/p>
*
* @author Oleg Kalnichevski
*
*/
public class FormDemo
{
//static final String LOGON_SITE = "developer.java.sun.com";

static final String LOGON_SITE = "localhost";



static final int LOGON_PORT = 8080;

public FormDemo() {
super();
}

public static void main(String[] args) throws Exception {

HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");

GetMethod authget = new GetMethod("/test/jsp/test.jsp");

client.executeMethod(authget);
System.out.println("Login form get: " + authget.getStatusLine().toString());
// release any connection resources used by the method
authget.releaseConnection();


PostMethod authpost = new PostMethod("/test/jsp/test.jsp");
// Prepare login parameters
NameValuePair action = new NameValuePair("FirstName", "Vijay");
NameValuePair url = new NameValuePair("LastName", "Chinna");
//NameValuePair action1 = new NameValuePair("Vijay", "FirstName");

//NameValuePair userid = new NameValuePair("UserId", "userid");
//NameValuePair password = new NameValuePair("Password", "password");
authpost.setRequestBody(
new NameValuePair[] {action, url});

client.executeMethod(authpost);
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();

// Usually a successful form-based login results in a redicrect to
// another url
int statuscode = authpost.getStatusCode();
System.out.println("statuscode: " + statuscode);

if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT) ||
(statuscode == 200)) {
Header header = authpost.getResponseHeader("location");
System.out.println("Header : " + header);

String newuri = "http://localhost:8080/test/jsp/test1.jsp?FirstName=Vijay&LastName=Chinna";

System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri);

client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
redirect.releaseConnection();

/*if (header != null) {
//String newuri = header.getValue();
System.out.println("Redirect target(newuri): " + newuri);

if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri);

client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
redirect.releaseConnection();
} else {
System.out.println("Invalid redirect");
System.exit(1);
}
*/
}
}
}
----------------------------------------------------------------

The jsps are

test.jsp
========
& lt;html>
& lt;head>

& lt;script>
function fnSubmit()
{

document.frm.action = "/test/jsp/test1.jsp";

}

& lt;/script>

& lt;/head>
& lt;body onload111="document.frm.submit()" >


& lt;form name="frm" method="post" action="http://localhost:8080/test/jsp/test1.jsp" >

& lt;%

System.out.println("I am Inside test.jsp");
String strFName = request.getParameter("FirstName");
String strLName = request.getParameter("LastName");

System.out.println("strFName Inside test.jsp: "+strFName);
System.out.println("strLName Inside test.jsp: "+strLName);

%>

First Name
& lt;INPUT class=field name="FirstName" value = & lt;%=strFName%> size="20">

Last Name
& lt;INPUT class=field name="LastName" value = & lt;%=strLName%> size="20">

& lt;INPUT class="btn" type="button" value=Submit name="B1" onClick111="fnSubmit();">

& lt;/form>
& lt;/body>


& lt;/html>

----------------------------


test1.jsp
=========
& lt;html>
& lt;head>

& lt;script>
function fnSubmit()
{


document.frm.action = "/test/ctrl?do=SUBMIT";
document.frm.submit();

}

& lt;/script>

& lt;/head>
& lt;body>
& lt;form name="frm" method="post" >
& lt;%
System.out.println("I am Inside test1.jsp");
String strFName = request.getParameter("FirstName");
String strLName = request.getParameter("LastName");

System.out.println("strFName Inside test1.jsp: "+strFName);
System.out.println("strLName Inside test1.jsp: "+strLName);
%>
Thanks
& lt;/form>
& lt;/body>


& lt;/html>

-----------------------------------------------------

This works ok. But I need to give
String newuri = "http://localhost:8080/test/jsp/test1.jsp?FirstName=Vijay&LastName=Chinna";

to make it work. If not its not working proper, ie, even though the test.jsp should goto test1.jsp automatically, it doesnt go. But when I just goto test.jsp directly, it takes me to test1.jsp.

What am I missing?


Also, now these are my jsps, so I can change it in anyway I want to get the parameter values. But how will i do that in other sites?

I know the method and action of the other site, but if I pass the values as I did now, will it work? But how will I make it to submit.
Thanks for the help
 
Space pants. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic