数据库交互后的重定向

时间:2015-04-24 15:27:57

标签: java oracle jsp servlets

这是我怀疑导致问题的servlet代码。将电影添加到数据库后,我无法弄清楚如何让servlet重定向回list.jsp页面。我认为它与doGet()方法有关。你能指出我正确的方向吗?我正在使用Netbeans,Tomcat和Oracle 11g XE。

package movies;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings({"unused", "serial"})
public class MovieAdd extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Connection c = null;
    Statement stmt = null;
    ResultSet rs = null;
    String sql;

    PrintWriter out = response.getWriter();
    response.setContentType("text/html");

    String movie = request.getParameter("movie");
    String year = request.getParameter("year");
    String rating = request.getParameter("rating");
    String actors = "";

    for (int l = 0; l < 5; l++) {
        String m = "a" + (l + 1);
        String actor = request.getParameter(m);
        if (actor != null && actor != "" && !actor.isEmpty()) {
            actor = actor + ", ";
            actors = actors + actor;
        }
    }
    if (actors.endsWith(", ")) {
        actors = actors.substring(0, actors.length() - 2);
    }
    System.out.println(actors);
    try {
        Class.forName("oracle.jdbc.OracleDriver");
        c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE", "system", "password");//PUT DATABASE CONNECTION INFO
        c.setAutoCommit(false);
        System.out.println("Opened database successfully");

        stmt = c.createStatement();

        sql = "INSERT INTO MOVIES (NAME, YEAR, RATING, ACTORS) VALUES ('"
                + movie
                + "','"
                + year
                + "','"
                + rating
                + "','"
                + actors
                + "')";
        stmt.executeUpdate(sql);
        c.commit();

        sql = "SELECT * FROM MOVIES";
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            String movieDb = rs.getString("NAME");
            int yearDb = rs.getInt("YEAR");
            int ratingDb = rs.getInt("RATING");
            int actorsDb = rs.getInt("ACTORS");
            System.out.println("Movie:- " + movieDb);
            System.out.println("Year of Release:- " + yearDb);
            System.out.println("Rating:- " + ratingDb);
            System.out.println("Actors:- " + actorsDb + "\n");
        }
        stmt.close();
        c.close();

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }

    String site = "list.jsp";
    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
    response.setHeader("Location", site);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String site = "add.jsp";
    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
    response.setHeader("Location", site);
}


}

2 个答案:

答案 0 :(得分:1)

先生。 Loachamin是完全正确的,我给了你答案,因为你对我最初的问题有正确的答案。我的代码的真正问题是在这个while循环期间,数据类型与数据库数据类型实际上并不匹配。

    while (rs.next()) {
        String movieDb = rs.getString("NAME");
        int yearDb = rs.getInt("YEAR");
        int ratingDb = rs.getInt("RATING");
        int actorsDb = rs.getInt("ACTORS");//Should be a string, not an int
        System.out.println("Movie:- " + movieDb);
        System.out.println("Year of Release:- " + yearDb);
        System.out.println("Rating:- " + ratingDb);
        System.out.println("Actors:- " + actorsDb + "\n");

答案 1 :(得分:-1)

您可以通过以下方式向jsp页面发送重定向:

String site = "list.jsp";
response.sendRedirect(site);

它会向浏览器发送一条重定向指令,对你传递的网址发出get请求。

您还可以使用forward对象的RequestDispatcher方法,主要区别在于此方法不会发送重定向响应,它会将请求处理“转发”到其他组件,就像一个重定向,但在服务器中。

String site = "list.jsp";
request.getRequestDispatcher("list.jsp").forward(request, response);

您必须记住,对于这两种方法,您不得提交响应,否则这些方法将抛出IllegalArgumentException,换句话说,在调用这些方法之前不会修改响应的内容。