日期:2014-05-19  浏览次数:20755 次

servlet怎么跳转到html
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();

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