servlet怎么跳转到html-CSDN论坛

 我来答
ty...c@163.com
2016-12-24 · TA获得超过418个赞
知道小有建树答主
回答量:918
采纳率:83%
帮助的人:430万
展开全部
html页面跳转到servlet是通过提交form的action到servlet的一个处理入口实现的。
1、页面html代码:

<html>
<body>
<form ACTION="./post2" METHOD="POST">
<input name="name" type="text" id="name"/>
<input name="send" type="submit" value="send"/>
</form>
</body>
<html>

2、在servlet中定义的处理post请求的方法:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.*;
public class post2 extends HttpServlet{
protected void doDo(HttpServletRequest request,HttpServletResponse response)
throws IOException{
获取form表单参数:
String name = request.getParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><BODY>");
out.println("<H2>hello "+name+"</H2>");
out.println("<BR><BR>");
out.println("info:");
out.println("<BR><BR>");
out.println("<H2>metoda GET</H2>");
out.println("<BR><BR>");
out.println("SERVER_NAME="+request.getServerName()+"<BR>");
out.println("REQUEST_METHOD="+request.getMethod()+"<BR>");
out.println("QUERY_STRING="+request.getQueryString()+"<BR>");
out.println("REMOTE_HOST="+request.getRemoteHost()+"<BR>");
out.println("REMOTE_ADDR="+request.getRemoteAddr());
out.println("</BODY></HTML>");
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException {
doDo(request,response);
}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException {
doDo(request,response);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式