简体   繁体   English

如何从Java Servlet重定向到HTML页面以及Servlet变量?

[英]How to redirect to a html page from a java servlet along with the variables from the servlet?

I want to open a html page from my java servlet and the variables of that servlet should be used to assign the form fields in the html page which is to be opened. 我想从我的Java servlet打开一个html页面,并且该servlet的变量应用于在要打开的html页面中分配表单字段。

This is my servlet 这是我的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 * from userdb where uname= '"+uname+"' and 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 to pass the above four variables (fname, lname, dept, mobno) to   an html page and assign these to a form field in html page and I need to open that html page from this servlet

    }
    catch(Exception e)
    {
        pw.println(e.toString());
    }
}
}

This is my html page which i want to open from the servlet 这是我要从servlet打开的html页面

<!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 to achieve this?? 如何实现这一目标? Any help would be appreciated!! 任何帮助,将不胜感激!!

Redirect will send a message to the browser telling it to send a get request request for a different url 重定向将向浏览器发送一条消息,告知该浏览器针对其他网址发送获取请求请求
So if you want data from the current request, you either have to include it as a query string in the redirect URL or save on in the session so the subsequent request can retrieve it. 因此,如果要从当前请求中获取数据,则必须将其作为查询字符串包含在重定向URL中,或者保存在会话中,以便后续请求可以检索它。

An alternative is to use a forward which will call another URL and include the response in the response for the current request. 一种替代方法是使用转发,该转发将调用另一个URL,并将响应包括在当前请求的响应中。

Embedding field values dynamically in the html page requires a technology like JSP, have a look at this . 在HTML页面中嵌入动态字段值需要像JSP技术,看看这个 You need a JSP engine like Tomcat or Jetty and then: 您需要一个JSP引擎,例如Tomcat或Jetty,然后:

  • rename your .html to .jsp 将.html重命名为.jsp
  • Add value='<%= request.fname %>" or similar to each of your <input > tags 添加value='<%= request.fname %>"或类似于每个<input >标签

Then in your servlet you could do a redirect by: 然后,您可以在servlet中通过以下方式进行重定向:

response.setStatus (303);
response.setHeader ("Location", jsppath+"? fname="+fname+"&lname="+lname+"&dept="+dept+"&mobno="+mobno);

This would be a rather odd JSP implementation as it is a bit inside out. 这将是一个相当奇怪的JSP实现,因为它有些内在。 As prashant said you should spend some time learning JSP. 粗鲁的说您应该花一些时间学习JSP。 or better still look at JavaEE 6 or later including JSF. 还是最好还是看一下JavaEE 6或更高版本(包括JSF)。

JSP is simpler to learn than JSF but JavaEE 6 is well worth the time. JSP比JSF更易于学习,但是JavaEE 6值得花时间。

It does look a bit like you need a dynamic Web application framework like JavaEE to do what you want. 看起来确实有点像您需要像JavaEE这样的动态Web应用程序框架来完成所需的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM