• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fill out a html-form using a servlet ?

 
Uwe Trostheide
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
as we all now its pretty easy to process data sent by an html form to a servlet.
But how to do it the other way round ?
I want to fill out a html form by a servlet. Instead of me typing all the data into a html form and pressing the send button (e.g. login into to a site) a servlet should do this for me.
Any hints or even example code ?
Thanks,
Uwe
 
Jason Brawner
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have the servlet generate the html form and put in the default values.
 
Sophia Choi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope u can get idea.
public class showItem extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
String itemNum =req.getParameter("iNum");
String dest;
if(itNum == NULL){
dest = "/MissingItem.jsp";
}else{
dest = "ShowItem.jsp";
// call beans to save data into Vector
ItemTable shipTable = ShipTable.getShipTable();
req.setAttribute("item",item);
}
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(dest);
dispatcher.forward(req,res);
}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you want to fake a POST from a FORM and submit it like a request to another application. Sure - no problem. Start by studying the content created by a plain HTML form and duplicating it in your servlet.
I don't think you will be able to do it the way you suggest by forwarding a request - instead you will have to open a HttpURLConnection.
Bill
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if Im reading this post right there is a little known way to dynamically handle variables in a plain jane html page... I actually just discovered this only a few months ago. If you send the info to the html page in a get string (IE: http://mySite/myPage.html?variable1=something&variable2=something) You can grab that query string with a javascript function called location.search & then fill in the form with javascript in the onLoad event of the page. Nice thing is it is 100% browser compatable (isnt that amazing )
I use this on leeritenour.com becuase I have to know the users screen width because the design layout will blow up if I miscalculate the resolution of the user. No matter what page they come in to I check for a session, if its not there I redirect them (after I have added the session) to a simple html page that grabs their resolution along with the page they came from & then create a redirect back to the page they requested, adding the resolution to the query string. They hit the servlet, the numbers are added to the session & the resolution width is passed to the servlet so I can do my calculations for the layout. Its very cool, I was thrilled when I found it & got it to work!
Point of this is, you CAN use variables in plain html...you just grab them & do your dynamic writing to the form via javascript.
So if this is what I think you are asking drag out the javascript book (I recommend O'Reilly) & start digging!
[ May 08, 2003: Message edited by: DC Dalton ]
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I posted above I realized some code might help (Once again I hope Im reading your question right) So here is the code from the servlet & the html file that grabs the variables & redirects back to the servlet:
From the servlet (check to see if I have the variables I need for layout)

Then from the html page (to grab the screen info & redirect back to the servlet that sent the request)
<SCRIPT LANGUAGE="JavaScript">
<!--
function getWidth(){
var width = screen.width;
var pageString = location.search;
var page = pageString.substring(6, pageString.indexOf("&"));
var servlet = pageString.substring(pageString.indexOf("&serv=") +6, pageString.length);
location.href = "/servlet/"+servlet+"?page="+page+"&width="+width;
}
//-->
</SCRIPT>
I call the getWidth() function in the onLoad event of this page & since the page has NOTHING on in it is almost unoticable to the user.
Once again, hope this helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!