Sunday, 7 May 2017
How do I split Java code away from a jsp page in NetBeans?
Answer
I was assigned a task of making a NetBeans jsp page to run on Tomcat so that the code interfaces and queries a MySql table in order to retrieve its data and display them on an IE page. I did that and the code is working when executed; I'm reproducing it (it's the PhoneBookTableDataRetrieval.jsp file) below:
<%@page import="java.sql.*"%>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%> //
//
<%!
public class Record {
String pathToDB = "jdbc:mysql://localhost:3306/agenda";
String username = "root";
String password = "Digital1";
Connection sessionWithAgendaDB = null;
PreparedStatement precompiledStatementToPhoneBookTable = null;
ResultSet queriedTableData = null;
public Record (){
try {
sessionWithAgendaDB =
DriverManager.getConnection(pathToDB, username, password);
precompiledStatementToPhoneBookTable
= sessionWithAgendaDB.prepareStatement("SELECT *
FROM phone_book");
} catch (SQLException e){
e.printStackTrace();
}
}
public ResultSet fetchRecords(){
try {
queriedTableData
= precompiledStatementToPhoneBookTable.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return queriedTableData;
}
}
%>
<%
Record record = new Record();
ResultSet records = record.fetchRecords();
%>
Phone Book Code
First Name
Last Name
Phone Number
<% while (records.next()) { %>
<%= records.getInt("p_B_CODE")%>
<%= records.getString("p_B_NAME")%>
<%= records.getString("p_B_SURNAME")%>
<%= records.getString("p_B_PHONE")%>
<% } %>
Yet I was told that my code was mixing together parts of Java code and parts of HTML code, which is true, and that wasn't good for modularity and portability purposes. So I was told to split the Java code of the classes I wrote inside the jsp page, and to save those classes as *.java files within the src/java folder of the NetBeans project, whose branching I cannot reproduce below with a screenshot, so I kind of plot it:
project Name: PhoneBookTableDataRetrieval, under which are the folders:
build
nbproject
src
conf
java
web
WEB-INF
PhoneBookTableDataRetrieval.jsp
build.xml
My problem is that I don't know neither how I have to write those *.java files in order to make them usable classes (are they a 'public static void main' thing or what ?), nor how I have to call them from within the HTML code in the jsp page (by the way, should I also split the HTML code from the jsp page and save it somewhere else too ? If so, I don't know either how to do that). Can you help me on this ? If you want really to do so, please, write completely the code that should make the *.java files (using of course the Java code of the classes I already wrote), and, please, write completely the statements that call those *.java files from within the jsp page. If you don't write explicitly the code, I won't be able to write it on my own; therefore directions like 'Yes, save the classes in a public static void main file and call them from within the jsp page with a "Load" statement and you'll be fine' are totally useless to me. I apologize in advance for my ignorance but, as I told you, I'm really a dummy programmer (yet this thing is important to me). Thank you really very so much for the detailed (written in a complete fashion) help you'll provide me with.
c++ - Does curly brackets matter for empty constructor?
Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...
-
A fair amount of the second act of The Dark Knight Rises has a class warfare plotline. This is foreshadowed in the trailers with Selina Ky...
-
I want to create an options array from a string. How can i create an array as { width : 100, height : 200 } from a string ...
-
I'm new to Programming. I would like to implement a program with a yield keyword . So That, I have created a new List and ask the user ...
No comments:
Post a Comment