You are on page 1of 10

INFO 3303 WEB PROGRAMMING II

Assignment 1

Lecturer: Madam Asma Md Ali

Section 1

Details: Nur Fatihah Binti Abu Bakar 1117818 Noorkhalida Binti Azmi 1113556

Use the IIUM student online course registration system. How to improve the system (using servlet and JSP)? How can the system be implemented using three-tier architecture? Java Servlet technology and JavaServer Pages (JSP pages) are server-side technologies that have dominated the server-side Java technology market. Servlets and JSP pages help separate presentation from content. Best practices are proven approaches for developing quality, reusable, and easily maintainable servlet- and JSP-based web applications. For instance, embedded Java code (scriptlets) in sections of HTML documents can result in complex applications that are not efficient, and difficult to reuse, enhance, and maintain. If you spend a lot of time re-running the same servlet/JSP, you can cache its results and return results out of the cache the next time it is run. This is useful for common queries that all visitors to your site run: you want the results of the query to be dynamic because the results might change daily, but you don't need to run the logic for every user. Similar to Common Gateway Interface (CGI) scripts, servlets support a request and response programming model. When a client sends a request to the server, the server sends the request to the servlet. The servlet then constructs a response that the server sends back to the client. Unlike CGI scripts, however, servlets run within the same process as the HTTP server. When a client request is made, the service method is called and passed a request and response object. The servlet first determines whether the request is a GET or POST operation. It then calls one of the following methods: doGet or doPost. The doGet method is called if the request is GET, and doPost is called if the request is POST. Both doGet and doPost take request ( HttpServletRequest) and response (HttpServletResponse). In the simplest terms, then, servlets are Java classes that can generate dynamic HTML content using print statements. Consequently, when you use servlets, you gain all the benefits from the Java platform, which include the sandbox (security), database access API via JDBC, and crossplatform portability of servlets. When a JSP page is called, it will be compiled (by the JSP engine) into a Java servlet. At this point the servlet is handled by the servlet engine, just like any other servlet. The servlet engine then loads the servlet class (using a class loader) and executes it to create dynamic HTML to be

sent to the browser. The servlet creates any necessary object, and writes any object as a string to an output stream to the browser. Advantages of JSP vs. JavaScript. JavaScript can generate HTML dynamically on the client. This is a useful capability, but only handles situations where the dynamic information is based on the client's environment. With the exception of cookies, HTTP and form submission data is not available to JavaScript. And, since it runs on the client, JavaScript can't access server-side resources like databases, catalogs, pricing information, and the like. vs. Static HTML. Regular HTML, of course, cannot contain dynamic information. JSP is so easy and convenient that it is quite feasible to augment HTML pages that only benefit marginally by the insertion of small amounts of dynamic data. Previously, the cost of using dynamic data would preclude its use in all but the most valuable instances. 3-Tier Architecture The simplest of N-Tier architecture is 3-Tier which typically contains following software component layers listed from the top level to the low level: presentation layer, application layer and data layer, which are depicted in Diagram 1. A layer can access directly only the public components of its directly-below layer. For example, presentation layer can only access the public components in application layer, but not in data layer. Application layer can only access the public components in data layer, but not in presentation layer. Doing so can minimize the dependencies of one layer on other layers. This dependency minimization will bring benefits for layer development/maintenance, upgrading, scaling and etc. Doing so also makes the tier security enforcement possible. For example, the client layer cannot access the data layer directly but through the application layer, so data layer has a higher security guarding. Finally, doing so can also avoid cyclic dependencies among software components.

In order to claim complete 3-Tier architecture, all three layers should be able to run in separate computers.

Diagram1: 3-Tier Architecture These three layers are briefly described as below: Presentation layer: a layer that users can access directly, such as desktop UI, web page and etc. It is also called as client. Application layer: this layer encapsulates the business logic (such as business rules and data validation), domain concept, data access logic and etc. It is also called as called middle layer. Data layer: the external data source to store the application data, such as database server, CRM system, ERP system, mainframe or other legacy systems and etc. The one we meet often today is database server. For N-Tier architecture, we need to use the non-embedded database server, such as SQL server, Oracle, DB2, MySQL or PostgreSQL. The non-embedded database server can be run in an individual computer. Whereas, the embedded type databases, such as Microsoft access, dbase and etc, cannot run in an individual computer, and then cannot be used as the data layer of the 3-Tier architecture.

Create a new web application for halaqah (usrah) monitoring system. The system should be able to register new member. The system should give points to the group member that attends the halaqah (usrah) and the leader of the group get half of the point. Write the code snippet.

register.html <html> <body> <form action=register method=post> <head><title>Registration</title></head> <BODY BGCOLOR="#FDF5E6"> Name : <input type=text name=regName/><br/> Matric No : <input type=text name=matricNo/><br/> Kulliyyah : <input type=text name=regKulliyyah/><br/> Email : <input type=text name=regEmail/><br/> <br/><br/> <input type=submit value=reg/> </form> </body> </html> register.java package register; import java.io.*; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.*; public class Register extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(text/html); PrintWriter out = response.getWriter();

String n=request.getParameter(regName); String m=request.getParameter(matricNo); String k=request.getParameter(regKulliyyah); String e=request.getParameter(regEmail); try{ Class.forName(oracle.jdbc.driver.OracleDriver); Connection con=DriverManager.getConnection( jdbc:oracle:thin:@localhost:1521:xe,system,oracle); PreparedStatement ps=con.prepareStatement( insert into regmember values ( , , , )); ps.setString(1,n); ps.setString(2,m); ps.setString(3,k); ps.setString(4,e); int i=ps.executeUpdate(); if(i>0) out.print(You have successfully registered.); }catch (Exception e2) {System.out.println(e2);} out.close(); } } attendance.jsp 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; import java.sql.*; public class attendance extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter(); int i; try { String d=request.getParameter("date"); String n=request.getParameter("matricNo"); String r=request.getParameter("regName"); String s=request.getParameter("status"); Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/scs","root","root"); Statement st=con.createStatement(); String sql="insert into attendance(date,matricNo,regName,status) values('"+d+"','"+n+"','"+r+"','"+s+"')"; st.executeUpdate(sql); st.close(); con.close(); if(status==Present){ request.setAttribute("Marks = 1"); else{ response.sendRedirect("Attendance.jsp"); } }catch(Exception e){ out.println(e); } finally { out.close(); } attendance.html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.8.3.js"></script> <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script> <link rel="stylesheet" href="/resources/demos/style.css" /> <script> $(function() { $('.datepicker').datepicker(); $.datepicker.formatDate('dd-mm-yy');

$('div.ui-datepicker').css({ fontSize: '12px' });

}); </script> <title>JSP Page</title> </head> <body> <h1 align="center">Employee Attendance</h1> <form action="Attendance" method="POST"> <table align="center" cellspacing="10" border="0"> <tr><td><strong>Date</strong></td><td><input type='text' class='datepicker' name='date'/></td></tr> <tr><td></td><td><strong>ID</strong></td><td><strong>Name</strong></td><td><str ong>Status</strong></td></tr> <% try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/scs","root","root"); Statement st=con.createStatement(); String sql="select * from staff"; ResultSet rs=st.executeQuery(sql); while(rs.next()){ String id=rs.getString("id"); String name=rs.getString("fname");
%>

<tr><td></td><td><input type="text"value="<%=id %>" disabled="true" /></td><td><input type="text" value="<%=name %>" name="name" disabled="true"/></td> <td><select name="status"> <option>Present</option> <option>Absent</option> <option>Leave</option> </select></td></tr> <% } }catch(Exception e){ out.println(e); }
%>

<tr><td></td><td></td><td><input type="Submit" value="Submit"/></td></tr> </table> </form> </body> </html>

Coding Error We did not manage to run the code. When user start to click register, error occurred. At first we cannot display even the form of the site.

You might also like