簡體   English   中英

如何從Java Servlet重定向到HTML頁面以及Servlet變量?

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

我想從我的Java servlet打開一個html頁面,並且該servlet的變量應用於在要打開的html頁面中分配表單字段。

這是我的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());
    }
}
}

這是我要從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>

如何實現這一目標? 任何幫助,將不勝感激!!

重定向將向瀏覽器發送一條消息,告知該瀏覽器針對其他網址發送獲取請求請求
因此,如果要從當前請求中獲取數據,則必須將其作為查詢字符串包含在重定向URL中,或者保存在會話中,以便后續請求可以檢索它。

一種替代方法是使用轉發,該轉發將調用另一個URL,並將響應包括在當前請求的響應中。

在HTML頁面中嵌入動態字段值需要像JSP技術,看看這個 您需要一個JSP引擎,例如Tomcat或Jetty,然后:

  • 將.html重命名為.jsp
  • 添加value='<%= request.fname %>"或類似於每個<input >標簽

然后,您可以在servlet中通過以下方式進行重定向:

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

這將是一個相當奇怪的JSP實現,因為它有些內在。 粗魯的說您應該花一些時間學習JSP。 還是最好還是看一下JavaEE 6或更高版本(包括JSF)。

JSP比JSF更易於學習,但是JavaEE 6值得花時間。

看起來確實有點像您需要像JavaEE這樣的動態Web應用程序框架來完成所需的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM