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.printIn("<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.printin("<tr><td>"+ID "</td>"+"<td>"+name +"</td>"+"<td>"+deptname +"<td><tr>; } out.printIn("</table>"); } else ..as above,but for instructors .. Database System Concepts-7th Edition 9.18 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 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 sessionis already active: if (request.getSession(false)==true) .then existing session else..redirect to authentication page 。authentication page check login/password Create new session HttpSession session request.getSession(true) Store/retrieve attribute value pairs for a particular session ·session.setAttribute(“userid'”,userid) ·If existing session: HttpSession request.getSession(false); String userid =(String)session.getAttribute("userid") Database System Concepts-7th Edition 9.19 ©Silberschat乜,Korth and Sudarshan
Database System Concepts - 7 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 ▪ Create new session • HttpSession session = request.getSession(true) ▪ Store/retrieve attribute value pairs for a particular session • session.setAttribute(“userid”, userid) • If existing session: HttpSession = request.getSession(false); String userid = (String) 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-7th Edition 9.20 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 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