160 likes | 262 Views
11 – Java Servlets. Session Aims & Objectives. Aims To cover a range of web-application design techniques Objectives, by end of this week’s sessions, you should be able to: create a servlet use a class to gather code common to different pages. most application programs – 3 major layers
E N D
Session Aims & Objectives • Aims • To cover a range of web-application design techniques • Objectives,by end of this week’s sessions, you should be able to: • create a servlet • use a class to gather code common to different pages
most application programs – 3 major layers Top (Presentation) layer: human/machine interaction (the user interface) input from the keyboard / mouse output in the form of screen displays / sound Middle (Application or business logic) layer: core functionality – gives application program its character contains business rules -> drive an organisation e.g. order entry system vs. inventory control system Bottom layer general services needed by other layers e.g. file, print, communications, and database services Application Layers 3
2-Tier Architecture Presentation and Application layer located on client machine could be implemented using Applet interacting server Known as a ‘fat client’
3-Tier Architecture 3-tier architecture, only presentation layer on client application layer on server Database on server or third machine Known as a ‘thin-client’ very little (application) code / processing on client e.g. use of Java Servlets (JSP pages)
Example: AddNum (JSP) AddNum.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <% double N1; double N2; String Res = ""; if (request.getParameter("btnAdd") != null){ N1 = Double.parseDouble(request.getParameter("txtN1")); N2 = Double.parseDouble(request.getParameter("txtN2")); Res = Double.toString(N1 + N2); } %> <!DOCTYPE html> <html> <head><title>Add Numbers</title></head> <body> <form method="post"> <input name="txtN1" type="text" /><br /> <input name="txtN2" type="text" /><br /> <input name="btnAdd" type="submit" value="Add" /> <p><%=Res%></p> </form> </body> </html> Java - functionality HTML – user interface
JSP pages & Servlets • all JSP pages converted to servlet • Servlet • Java program running in web server • Special type of Java class (.java file) • Can get servlet error – caused by error in JSP page (usually missing } ), but difficult to see the connection
AddNum: Servlet (.html file) • Split • User interface (html) • Functionality (Java) <!DOCTYPE html> <html> <head><title>Add Numbers</title></head> <body> <form method="post" action="AddNum"> <input name="txtN1" type="text" /><br /> <input name="txtN2" type="text" /><br /> <input name="btnAdd" type="submit" value="Add" /> </form> </body> </html> Points to Servlet (.java)
AddNum: Servlet (.java file) import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddNum extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { double N1; double N2; String Res = ""; response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { N1 = Double.parseDouble(request.getParameter("txtN1")); N2 = Double.parseDouble(request.getParameter("txtN2")); Res = Double.toString(N1 + N2); out.println("<html>"); out.println("<head>"); out.println("<title>Add Numbers</title>"); out.println("</head>"); out.println("<body>"); out.println(Res); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Short description"; } } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { double N1; double N2; String Res = ""; response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { N1 = Double.parseDouble(request.getParameter("txtN1")); N2 = Double.parseDouble(request.getParameter("txtN2")); Res = Double.toString(N1 + N2); out.println("<html>"); out.println("<head>"); out.println("<title>Add Numbers</title>"); out.println("</head>"); out.println("<body>"); out.println(Res); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } Calculationcode Also, write html
Example: PeopleList.jsp v2 <%@page import="java.sql.*"%> <%@page contentType="text/html"%> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; String id; while(r.next()){ id = Integer.toString(r.getInt("PersonID")); html += "<a href='Person2.jsp?id=" + id + "'>"; html += r.getString("Surname") + "</a><br />"; } cn.close(); %> <!DOCTYPE html> <html> <head><title></title></head> <body> <%=html%> </body> </html> Connect to db
Example: Person.jsp v2 <%@page import="java.sql.*"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% String id = request.getParameter("id"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person WHERE PersonID = " + id + ";"); String surname = ""; if(r.next()){ surname = r.getString("Surname"); } cn.close(); %> <!DOCTYPE html> <html> <head><title>Person</title></head> <body> Surname: <input name="txtSurname" type="text" value="<%=surname%>" /> </body> </html> Connect to DB
Person & PeoplList v2 <%@page import="java.sql.*"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% String id = request.getParameter("id"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person WHERE PersonID = " + id + ";"); String surname = ""; if(r.next()){ surname = r.getString("Surname"); } cn.close(); %> <!DOCTYPE html> <html> <head><title>Person</title></head> <body> Surname: <input name="txtSurname" type="text" value="<%=surname%>" /> </body> </html> • both JSP page duplicate common code <%@page import="java.sql.*"%> <%@page contentType="text/html"%> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; String id; while(r.next()){ id = Integer.toString(r.getInt("PersonID")); html += "<a href='Person2.jsp?id=" + id + "'>"; html += r.getString("Surname") + "</a><br />"; } cn.close(); %> <!DOCTYPE html> <html> <head><title></title></head> <body> <%=html%> </body> </html>
Class People • Contains common code for both pages People cn st r Open Select Close
Class: People.java 1 • Common code package Main; import java.sql.*; public class People{ private Connection cn; private Statement st; private ResultSet r; public void Open(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); } catch (Exception e){ //e.printStackTrace(); } } public void Select(String sql){ try{ st = cn.createStatement(); r = st.executeQuery(sql); } catch (Exception e){ //e.printStackTrace(); } } package Main; import java.sql.*; public class People{ private Connection cn; private Statement st; private ResultSet r; public void Open(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); } catch (Exception e){ //e.printStackTrace(); } } public void Select(String sql){ try{ st = cn.createStatement(); r = st.executeQuery(sql); } catch (Exception e){ //e.printStackTrace(); } } public boolean Next(){ boolean found = false; try{ found = r.next(); } catch (Exception e){ //e.printStackTrace(); } return found; } public String get(String id){ String s = ""; try{ s = r.getString(id); } catch (Exception e){ //e.printStackTrace(); } return s; } public void Close(){ try{ cn.close(); } catch (Exception e){ //e.printStackTrace(); } } }
Class: People.java 2 • Common code public boolean Next(){ boolean found = false; try{ found = r.next(); } catch (Exception e){ //e.printStackTrace(); } return found; } public String get(String id){ String s = ""; try{ s = r.getString(id); } catch (Exception e){ //e.printStackTrace(); } return s; } public void Close(){ try{ cn.close(); } catch (Exception e){ //e.printStackTrace(); } } } package Main; import java.sql.*; public class People{ private Connection cn; private Statement st; private ResultSet r; public void Open(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); } catch (Exception e){ //e.printStackTrace(); } } public void Select(String sql){ try{ st = cn.createStatement(); r = st.executeQuery(sql); } catch (Exception e){ //e.printStackTrace(); } } public boolean Next(){ boolean found = false; try{ found = r.next(); } catch (Exception e){ //e.printStackTrace(); } return found; } public String get(String id){ String s = ""; try{ s = r.getString(id); } catch (Exception e){ //e.printStackTrace(); } return s; } public void Close(){ try{ cn.close(); } catch (Exception e){ //e.printStackTrace(); } } }
PersonList.jsp <%@page import="Main.*" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%! People p = new People(); %> <% String html = ""; p.Open(); p.Select("SELECT * FROM Person;"); while(p.Next()){ html += p.get("Surname") + "<br />"; } p.Close(); %> <!DOCTYPE html> <html> <head><title>People</title></head> <body> <%=html%> </body> </html> • Class complex • Pages simpler Import Package Create Instance Use methods