servlet怎么跳转到html-CSDN论坛
展开全部
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);
}
}
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询