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();

解决方案 »

  1.   

    想要做跳转,必须在你没有向 response 输出任何东西之前,调用:
    response.sendRedirect("/xxoo/xxoo.html");
      

  2.   


    有两种跳转方法  在这位仁兄的基础上还有个 转发 是request.getRequestDispatcher方法 他说的是重定向 具体可以查一下文档说明
      

  3.   

    request.getRequestDispatcher("userAdmin.jsp//xxoo/xxoo.html").forward(request, response);
      

  4.   

    有两种页面跳转方式;
           服务器端跳转:<jsp:forward> 特点:无条件的跳转,只要执行到语句就跳转,可以传递response范围的属性.
             //request.getRequestDispatcher("../index.jsp").forward(request,response);这是在Servlet中的页面跳转。
           客户端跳转:response.sendRedirect()、setHeader();特点:所有页面执行完在跳转,不能传递response范围的属性。
      

  5.   


    是这样的。
    如果你将数值存入request范围,那么就不要使用redirect跳转,否则会无法获取到servlet中存储的数值。
      

  6.   

    response.sendRedirect("/xxoo/xxoo.html");
      

  7.   

    楼主 那个超链接写了 但是不能跳转莫?是跳到 哪个 html 哦