Answer the question
In order to leave comments, you need to log in
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;
}
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);
}
}
request.setAttribute();
request.getRequestDispatcher("index.jsp").forward(request, response);
request.setAttribute("p", new PrintText());
request.setAttribute("s", new ShowText());
request.setAttribute("e", new ShowText1());
request.getRequestDispatcher("index.jsp").forward(request, response);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question