【问题标题】:how to make servlets send applets to the same html page that has the form如何使 servlet 将小程序发送到具有表单的同一 html 页面
【发布时间】:2015-02-01 17:42:51
【问题描述】:

我对此非常陌生。是的,这是一个家庭作业。 我正在尝试制作一个带有表单的 HTML 页面,以将一些变量发送到 servlet,并且 servlet 将一个小程序发送到同一个表单页面。到目前为止,我成功地单独发送了小程序。我尝试使用 jQuery,但无法发送 HTML 标签。我只能发短信。

到目前为止我的代码:

index.html

<html>
 <head>
  <title> title </title>
 </head>
 <body>
  <form action = "Servlet">
  <input type="text" name="Name" value="" size="5" />
  <input type="submit" value="Submit" name="Submit" />
</form> 
</body>

Servlet.java

    package ServletPackage;

import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;


public Servlet() {
    super();
    // TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    String nm;


    nm = request.getParameter ("Name");



    PrintWriter mess = response.getWriter();

    mess.println("<html>");
    mess.println("<head>");
    mess.println("</head>");
    mess.println("<body>");
    mess.println("<b> You've entered Name: </b>" + nm);


    mess.println("<p align=center>");
    mess.println("<applet code='NameInShape.class' width=200 height=200>");
    mess.println("<param name='NAME'      value='" + nm + "'>");
    mess.println(" </applet>");
    mess.println("</p>");
    mess.println("</html>");
    mess.println("</body>");
    mess.close();
}


public static void main( String[] args )

{   }


/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

感谢所有帮助

【问题讨论】:

    标签: java jquery html ajax servlets


    【解决方案1】:

    一种方法是使用RequestDispatcher 调用index.html 的.jsp(JavaServer Pages)克隆,并使用表单发布的参数。不用担心,实现起来非常简单。结果将看起来像表单仍然可用的初始页面,但将使用指定的参数调用小程序。

    您需要导入 RequestDispatcher (import javax.servlet.RequestDispatcher;),然后您可以在 doPost 方法中执行类似的操作:

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    
        RequestDispatcher rd = request.getRequestDispatcher("clone.jsp");
        rd.forward(request, response);
    
    }
    

    rd.forward(request, response); 方法使发布到您的 servlet 的参数在 clone.jsp 页面上可用,您可以使用 &lt;%=request.getParameter("ParameterName")%&gt; 在 .jsp 页面中插入它们。

    例如,在您的 servlet 中添加上述代码后,您可以在 clone.jsp 页面中插入类似这样的内容,以嵌入带有从 index.html 表单提交中收到的参数的小程序:

    <applet code=Applet.class width=300 height=300>
        <param name=NAME value="<%=request.getParameter("NAME")%>">
    </applet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多