servlet怎么跳转到html
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String slogan = request.getParameter("slogan");
String leader = request.getParameter("leader");
Connection cn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bbs";
String user = "root";
String password = "root";
cn = DriverManager.getConnection(url, user, password);
stmt = cn.createStatement();
stmt.executeUpdate("insert into team(name,slogan,leader) values('"+ name +"' , '"+ slogan +"' , '"+ leader +"')");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>增加小组成功</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>增加小组成功</h1>");
out.println("<p><a href='../addTeam.html'>继续增加</a>");
out.println("<a href='viewTeams'>显示小组</a>");
out.println("<a href='../hello.html'>返回首页</a>");
out.println("</body>");
out.println("</html>");
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>增加小组失败</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>增加小组失败</h1>");
out.println("<p><a href='../addTeam.html'>继续增加</a>");
out.println("<a href='../viewTeams'>显示小组</a>");
out.println("<a href='../hello.html'>返回首页</a>");
out.println("</body>");
out.println("</html>");
} finally {
try {
stmt.close();
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String slogan = request.getParameter("slogan");
String leader = request.getParameter("leader");
Connection cn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bbs";
String user = "root";
String password = "root";
cn = DriverManager.getConnection(url, user, password);
stmt = cn.createStatement();
stmt.executeUpdate("insert into team(name,slogan,leader) values('"+ name +"' , '"+ slogan +"' , '"+ leader +"')");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>增加小组成功</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>增加小组成功</h1>");
out.println("<p><a href='../addTeam.html'>继续增加</a>");
out.println("<a href='viewTeams'>显示小组</a>");
out.println("<a href='../hello.html'>返回首页</a>");
out.println("</body>");
out.println("</html>");
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>增加小组失败</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>增加小组失败</h1>");
out.println("<p><a href='../addTeam.html'>继续增加</a>");
out.println("<a href='../viewTeams'>显示小组</a>");
out.println("<a href='../hello.html'>返回首页</a>");
out.println("</body>");
out.println("</html>");
} finally {
try {
stmt.close();
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.sendRedirect("/xxoo/xxoo.html");
有两种跳转方法 在这位仁兄的基础上还有个 转发 是request.getRequestDispatcher方法 他说的是重定向 具体可以查一下文档说明
服务器端跳转:<jsp:forward> 特点:无条件的跳转,只要执行到语句就跳转,可以传递response范围的属性.
//request.getRequestDispatcher("../index.jsp").forward(request,response);这是在Servlet中的页面跳转。
客户端跳转:response.sendRedirect()、setHeader();特点:所有页面执行完在跳转,不能传递response范围的属性。
是这样的。
如果你将数值存入request范围,那么就不要使用redirect跳转,否则会无法获取到servlet中存储的数值。