V
V
Viktoria Smirnova2017-08-23 07:20:35
Java
Viktoria Smirnova, 2017-08-23 07:20:35

Please explain the implementation of class methods in JSP from a servlet?

Guys, please clarify the following: in my web application, there are three simple queries to the database SELECT, INSERT and UPDATE.

Accordingly, I have three classes with different functionality to the database.

SELECT example:

class DirectorSelect{
  public String selectDirectors() {
    Connection koneksi = null;
    Statement stat = null;
    String str = "";
    try{
        Class.forName("com.mysql.jdbc.Driver");
        koneksi = DriverManager.getConnection("jdbc:mysql://localhost/dbizza","root","");
        stat = koneksi.createStatement();
        ResultSet hasil = stat.executeQuery("SELECT * FROM directors");
        while (hasil.next()) {
            str = str + (hasil.getInt(1) + "\t" + hasil.getString(2)+ "\t" + hasil.getString(3)+ "\t" + hasil.getDate(4)+ "\t" + hasil.getString(5));
        }
        stat.close();
        koneksi.close();
    } catch (SQLException sqle) {
        str = "SQLException error";
    } catch (ClassNotFoundException cnfe) {
        str = "ClassNotFoundException error";
    }
    return str;
 }


And one servlet:

public class DirectorsViewServlet extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    DirectorSelect ds = new DirectorSelect();
    out.println(ds.selectDirectors());
  }

  public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    doGet(req, res);
  }
}


Question 1 how in servlet using

request.setAttribute();
request.getRequestDispatcher("index.jsp").forward(request, response);


pass method value to JSP.

Question number 2 how to implement the remaining two methods of the two classes in the servlet, but that they would not be executed all together, as in the

example:
request.setAttribute("p", new PrintText());
request.setAttribute("s", new ShowText());
request.setAttribute("e", new ShowText1());
request.getRequestDispatcher("index.jsp").forward(request, response);


but only on request, possibly sent to different jsp pages. Or please clarify that my understanding of the logic of the servlet is not correct? Thank you all very much.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question