Java 如何使servlet将小程序发送到具有表单的同一html页面
我对这个很陌生。是的,这是一个家庭作业。 我试图用一个表单创建一个HTML页面,向servlet发送一些变量,servlet向同一表单页面发送一个applet。到目前为止,我成功地单独发送了applet。我尝试使用jQuery,但无法发送HTML标记。我只能发短信 到目前为止,我的代码是: index.htmlJava 如何使servlet将小程序发送到具有表单的同一html页面,java,jquery,html,ajax,servlets,Java,Jquery,Html,Ajax,Servlets,我对这个很陌生。是的,这是一个家庭作业。 我试图用一个表单创建一个HTML页面,向servlet发送一些变量,servlet向同一表单页面发送一个applet。到目前为止,我成功地单独发送了applet。我尝试使用jQuery,但无法发送HTML标记。我只能发短信 到目前为止,我的代码是: index.html <html> <head> <title> title </title> </head> <body>
<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
}
}
package-ServletPackage;
导入java.awt.Font;
导入java.io.IOException;
导入java.io.PrintWriter;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
公共类Servlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
公共Servlet(){
超级();
//TODO自动生成的构造函数存根
}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
response.setContentType(“text/html”);
字符串nm;
nm=request.getParameter(“名称”);
PrintWriter mess=response.getWriter();
mess.println(“”);
mess.println(“”);
mess.println(“”);
mess.println(“”);
println(“您已输入名称:”+nm);
mess.println(“”);
mess.println(“”);
mess.println(“”);
mess.println(“”);
mess.println(“
”);
mess.println(“”);
mess.println(“”);
mess.close();
}
公共静态void main(字符串[]args)
{ }
/**
*@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
//TODO自动生成的方法存根
}
}
一种方法是使用表单发布的参数调用index.html的.jsp(JavaServerPages)克隆。别担心,实现起来很简单。结果看起来像表单仍然可用的初始页面,但小程序将使用指定的参数调用 您需要导入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(请求、响应)
使发布到servlet的参数在clone.jsp
页面上可用,您可以使用
在.jsp页面中插入它们
例如,在servlet中添加上述代码后,您可以在clone.jsp
页面中插入类似的内容,以将小程序嵌入从index.html
表单提交中收到的参数:
<applet code=Applet.class width=300 height=300>
<param name=NAME value="<%=request.getParameter("NAME")%>">
</applet>