当前位置: 技术问答>java相关
如何在打开html页面是调用servlet?
来源: 互联网 发布时间:2017-04-18
本文导语: 我需要在打开index.html时调用一个servlet,servlet执行完后把参数返回index.html,让index.html显示出来,请问该如何做? | index.html ------------------------- --------------------------- myServlet ------------------ import java...
我需要在打开index.html时调用一个servlet,servlet执行完后把参数返回index.html,让index.html显示出来,请问该如何做?
|
index.html
-------------------------
---------------------------
myServlet
------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class myServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
String s="";
s+="";
s+="";
s+="test";
s+="";
s+="";
s+="";
s+="test";
s+="";
s+="";
out.println("parent.a.innerHTML='"+s+"';");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
}
---------------------
编译,在浏览器输入http://localhost:8080/index.html
我这里测试通过!
ok???
-------------------------
---------------------------
myServlet
------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class myServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
String s="";
s+="";
s+="";
s+="test";
s+="";
s+="";
s+="";
s+="test";
s+="";
s+="";
out.println("parent.a.innerHTML='"+s+"';");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
}
---------------------
编译,在浏览器输入http://localhost:8080/index.html
我这里测试通过!
ok???
|
在你放入tomcat 时,应修改你的web-inf 目录的web.xml.
可C:tomcatwebappsexamplesWEB-INF参考目录中web.xml.
可C:tomcatwebappsexamplesWEB-INF参考目录中web.xml.
|
|
使用JAVASCRIPT自动提交HTML的表单,action指向你的servlet,然后再由你的servlet调用你的index.html同时也可以传递参数。