How to redirect to a html page from a java servlet along with the variables from the servlet? -
i want open html page java servlet , variables of servlet should used assign form fields in html page opened.
this servlet
import java.io.*; import java.sql.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class signin extends httpservlet { public void dopost(httpservletrequest req,httpservletresponse res)throws ioexception,servletexception { printwriter pw=res.getwriter(); string fname=null,lname=null,uname,pwd,dept=null,mobno=null; connection con=null; statement stat=null; resultset rs=null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con=drivermanager.getconnection("jdbc:odbc:userdb"); stat=con.createstatement(); uname=req.getparameter("un"); pwd=req.getparameter("password"); //pw.println(pwd); rs=stat.executequery("select * userdb uname= '"+uname+"' , pwd='"+pwd+"'"); while(rs.next()) { fname=rs.getstring("fname"); lname=rs.getstring("lname"); dept=rs.getstring("dept"); mobno=rs.getstring("mobno"); } //pw.println(fname+" "+lname+" "+dept+" "+mobno); //i want pass above 4 variables (fname, lname, dept, mobno) html page , assign these form field in html page , need open html page servlet } catch(exception e) { pw.println(e.tostring()); } } }
this html page want open servlet
<!doctype html> <html> <head> <title></title> <link rel="stylesheet" type="text/css"href="sty2.css"/> </head> <body> <div id="bg"><img src="bg.jpg" width="100%" height="100%" alt=""></div> <div id="content"> <form name="data" action=""> <div id="header"> <h1>text</h1> </div> <div id="header1"> <h2> welcome </h2> </div> <div id="header2"> <input type="button" name="logout" value="logout" id="dlbutton" onclick="window.open('main.html')"/> </div> <div id="nav"> <br/> first name:<br/><br/> last name:<br/><br/> department :<br/><br/> mobile no.:<br/><br/> </div> <div id="section"> <br/> <input class="textbox" type="text" name="fn" placeholder="first name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'first name'"/><br/> <br/> <input class="textbox" type="text" name="ln" placeholder="last name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'last name'" /><br/><br/> <input class="textbox" type="text" name="dn" placeholder="dept name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'dept name'" /><br/><br/> <input class="textbox" type="text" name="mn" placeholder="mobile number" onfocus="this.placeholder = ''" onblur="this.placeholder = 'mobile number'" /><br/><br/> </div> <div id="footer"> <input type="submit" name="update" value="update" id="dlbutton"/> </form> </div> </div> </body> </html>
how achieve this?? appreciated!!
redirect send message browser telling send request request different url
if want data current request, either have include query string in redirect url or save on in session subsequent request can retrieve it.
an alternative use forward call url , include response in response current request.
embedding field values dynamically in html page requires technology jsp, have @ this. need jsp engine tomcat or jetty , then:
- rename .html .jsp
- add
value='<%= request.fname %>"
or similar each of<input >
tags
then in servlet redirect by:
response.setstatus (303); response.setheader ("location", jsppath+"? fname="+fname+"&lname="+lname+"&dept="+dept+"&mobno="+mobno);
this rather odd jsp implementation bit inside out. prashant said should spend time learning jsp. or better still @ javaee 6 or later including jsf.
jsp simpler learn jsf javaee 6 worth time.
it bit need dynamic web application framework javaee want.
Comments
Post a Comment