Example Servlet Code import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class PersonQueryServlet extends HttpServlet public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out response.getWriter(); out.println("<HEAD><TITLE>Query Result</TITLE></HEAD>"); out.printIn("<BODY>"); ....BODY OF SERVLET (next slide)... out.printIn("</BODY>"); out.close(; } } Database System Concepts-6th Edition 9.17 ©Silberschat乜,Korth and Sudarshan
Database System Concepts - 6 9.17 ©Silberschatz, Korth and Sudarshan th Edition Example Servlet Code import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class PersonQueryServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HEAD><TITLE> Query Result</TITLE></HEAD>"); out.println("<BODY>"); ….. BODY OF SERVLET (next slide) … out.println("</BODY>"); out.close(); } }
Example Servlet Code String persontype request.getParameter("persontype"); String number request.getParameter("name"); if(persontype.equals("student")){ ..code to find students with the specified name .. ..using JDBC to communicate with the database.. out.println("<table BORDER COLS=3>"); out.println("<tr><td>ID</td><td>Name:</td>"+"<td>Department</td></tr>"); for(.each result…X ..retrieve ID,name and dept name ..into variables ID,name and deptname out.printin("<tr><td>"+ID +"</td>"+"<td>"+name +"</td>"+"<td>"+deptname "</td></tr>"); } out.printin("</table>"); } else ..as above,but for instructors .. } Database System Concepts-6th Edition 9.18 ©Silberschat乜,Korth and Sudarshan
Database System Concepts - 6 9.18 ©Silberschatz, Korth and Sudarshan th Edition Example Servlet Code String persontype = request.getParameter("persontype"); String number = request.getParameter("name"); if(persontype.equals("student")) { ... code to find students with the specified name ... ... using JDBC to communicate with the database .. out.println("<table BORDER COLS=3>"); out.println(" <tr> <td>ID</td> <td>Name: </td>" + " <td>Department</td> </tr>"); for(... each result ...){ ... retrieve ID, name and dept name ... into variables ID, name and deptname out.println("<tr> <td>" + ID + "</td>" + "<td>" + name + "</td>" + "<td>" + deptname + "</td></tr>"); }; out.println("</table>"); } else { ... as above, but for instructors ... }
Servlet Sessions Servlet API supports handling of sessions Sets a cookie on first interaction with browser,and uses it to identify session on further interactions To check if session is already active: if (request.getSession(false)==true) .then existing session else..redirect to authentication page authentication page check login/password request.getSession(true):creates new session Store/retrieve attribute value pairs for a particular session session.setAttribute(“userid”,userid) session.getAttribute("userid") Database System Concepts-6th Edition 9.19 @Silberschatz,Korth and Sudarshan
Database System Concepts - 6 9.19 ©Silberschatz, Korth and Sudarshan th Edition Servlet Sessions Servlet API supports handling of sessions Sets a cookie on first interaction with browser, and uses it to identify session on further interactions To check if session is already active: if (request.getSession(false) == true) .. then existing session else .. redirect to authentication page authentication page check login/password request.getSession(true): creates new session Store/retrieve attribute value pairs for a particular session session.setAttribute(“userid”, userid) session.getAttribute(“userid”)
Servlet Support Servlets run inside application servers such as Apache Tomcat,Glassfish,JBoss BEA Weblogic,IBM WebSphere and Oracle Application Servers Application servers support deployment and monitoring of servlets Java 2 Enterprise Edition(J2EE)platform supporting objects, parallel processing across multiple application servers,etc Database System Concepts-6th Edition 9.20 @Silberschatz,Korth and Sudarshan
Database System Concepts - 6 9.20 ©Silberschatz, Korth and Sudarshan th Edition Servlet Support Servlets run inside application servers such as Apache Tomcat, Glassfish, JBoss BEA Weblogic, IBM WebSphere and Oracle Application Servers Application servers support deployment and monitoring of servlets Java 2 Enterprise Edition (J2EE) platform supporting objects, parallel processing across multiple application servers, etc
Server-Side Scripting Server-side scripting simplifies the task of connecting a database to the Web Define an HTML document with embedded executable code/SQL queries. Input values from HTML forms can be used directly in the embedded code/SQL queries. When the document is requested,the Web server executes the embedded code/SQL queries to generate the actual HTML document. Numerous server-side scripting languages JSP.PHP General purpose scripting languages:VBScript,Perl,Python Database System Concepts-6th Edition 9.21 @Silberschatz,Korth and Sudarshan
Database System Concepts - 6 9.21 ©Silberschatz, Korth and Sudarshan th Edition Server-Side Scripting Server-side scripting simplifies the task of connecting a database to the Web Define an HTML document with embedded executable code/SQL queries. Input values from HTML forms can be used directly in the embedded code/SQL queries. When the document is requested, the Web server executes the embedded code/SQL queries to generate the actual HTML document. Numerous server-side scripting languages JSP, PHP General purpose scripting languages: VBScript, Perl, Python