Vous êtes sur la page 1sur 4

ervlet and JSP is mostly used for generating dynamic webpages.

Static webpages are the opposite of dynamic


webpages. Describe static webpages.
 HTML is the language that the web browser converts into the webpages of a web application.
 A static webpage in HTML document that is stored in a file and doesn’t change in response to user input.
 HTTP is the protocol that the web browsers and web servers use to communicate.
 A web browser requests a page from a web server by sending the server a message known as an HTTP request. For
a static webpage, the HTTP request includes the name of the HTML file requested.
 A web server replies to an HTTP request by sending a message known as HTTP response back to the browser. For a
static Webpage, the HTTP response includes the HTML document.
1. Dynamic webpages make the internet more interactive, allowing real time update and user input. How it works.
 A dynamic webpage in an HTML document that’s generated by a web application. The webpage changes accordingly
to parameters that are sent to the web application by the web browser.
 When a web server receives a request for a dynamic webpage, the server passes the request to the web application.
Then, the application generates a response, which is usually an HTML document, and returns it to the web server.
 The web server, in turn wraps the generated HTML document in an HTTP response and sends it back to the browser.
 The browser doesn’t know or care whether the HTML was retrieved from a static HML file or was dynamically
generated by the web application. Either way, the browser displays the HTML document that is returned.
2. Applets are Java programs. However due to security concerns certain restrictions are imposed list them.
 Applets don’t use the main () method, but when they are load, automatically call certain methods (init, start, stop).
 They are embedded inside a webpage and executed in browsers.
 They cannot read from or write to the files on local computer.
 They cannot communicate with other servers on the network.
 They cannot run any program from the local computer.
 They are restricted from using libraries from other languages.
3. Explain FOUR (4) main benefits of writing code using JavaBeans.
 A bean provides all the benefits of Java "write once, run anywhere” paradigm
 The properties, events, and methods of a bean that are exposed to another application can be controlled
 A bean may register to receive events from other objects and can generate events that are sent to those other objects
 Auxiliary software can be provided to help a person configure a java bean. The configuration setting of being can be
saved in a persistent storage and restored later. Beans help to improve code readability.
 Beans help to improve code readability.
4. Describe each component of the MVC model
Ans:
 Model:
Model is a collection of Java classes that form a software application tended to store, and optionally separate, data.
A single front-end class that can communicate with any user interface (for example: a console a graphical user
interface, or a web application).
 View
View is represented by a java Server Pages, with data being transported the page in the HttpServletRequest and
HttpSession.
 Controller
Controller Servlet communicates with the front end of the model and loads the HTTPServletRequest or HTTPSession
with appropriate data, before forwarding the HTTPServletRequest and Response to the JSP using a
RequestDispatcher.
5. Evaluate the differences between a forward request and send Redirect
When you invoke a forward request, the request is sent to another resource on the server without the client being informed
that a different resource is going to process the request.
When a sendRedirect method is invoked, it causes the web container to return to the browser indicating that a new URL
should be requested
Been use the browser issues a completely new request any object that are
stored as request attributes before the redirect occurs will be lost
This extra round trip a redirect is slower than forward.
This process occurs completely with in the web container.
6. Describe the difference between doGET() and doPOST().
doGET() method is limited with 2k of data to be sent, and doPOST() method doesn’t have this limitation.
doPOST() method call doesn’t need a long text tail after a servlet name in a request. All parameters are stored in a request
itself, not in a request string, and it’s impossible to guess the data transmitted to a servlet only looking at a request string.
doPOST() is known to have better security than doGET() for data protection.
7. Explain the following JSP tags and their purposes.
i. JSP Scriplet
Tag: <% %> Purpose: To insert a block of Java Statements
ii. JSP expression
Tag: <%= %> Purpose: To display the string value of an expression
iii. JSP directive
Tag: <% @ %> Purpose: To set conditions that apply to the entire JSP.
iv. JSP comment
Tag: <%@-- --%> Purpose: To tell the JSP engine to ignore code
v. JSP declaration
Tag: <%@! %> Purpose: To declare instance variables and methods for a JSP.
8. Three reasons for the need to build dynamic webpages. Provide an example for each reason.
i. The webpage is based on data submitted by the user. Example: Results page from search engines and order
confirmation pages at online stores.
ii. The webpages is derived from data that change frequently. Example: A weather report or news headlines page.
iii. The webpage that need to use information from databases or other server-side sources. Example: An e-commerce site
could use a servlet to display current price and stock availability.
9. Two comments tags used to add comments to code in JSP page.
i. <% - - JSP Comment - - %>
ii. < ! - - HTML Comment - - >
10. Complete servlet program below to display the ODD numbers between 1 and 20 with its cube as shown.
Number 1 3 5 7 9 11 13 15 17
Cube 1 27 125 343 729 1331 2197 3375 4913

Response.setContentType(“text/html”); out.println(“</tr>”);
PrintWriter out = response.getWriter(); out.printIn(“<td>Cube</td>”);
Out.println (“<html>”); for (int i = 0; i <= 20; i++) {
Out.println (“<head><title>ODD Numbers</title></head>”); if ( i % 2== 1){
Out.println(“<body>”); out.printIn(“<td>” + I * I * “</td>”);
Out.println(“<table border=\”1\”>”); }
Out.println(“<tr>”); }
Out.println(“<td>Number</td>”); out.printIn(“</tr>”);
For (int i=0; I < = 20; i++ ) { out.printIn(“</body></html>”);
If ( i%2 = = 1) {
Out.println(“<td>” + I + “</td>”);
}
}

11. To display even numbers from 1 to 20 and its square root, square and cube as shown.
Number 0 2 4 6 8 10 12 14 16
Square 0 4 16 26 64 100 144 196 256
Cube 0 8 64 216 512 1000 172 2744 4096

Response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
Out.println(“<head>><title>Even, Squared and Cubed</title></head>”);
Out.println(“<body>”);
Out.println(“<table border=\”1\”>”);
Out.println(“<tr>”);
Out.println(“<td>Number</td>”); Private int computeSquare (int n) {
Out.println(“<td>Square Root</td>”); Return n*n;
Out.println(“<td>Square</td>”); }
Out.println(“<td>Cube</td>”); Private int computeCU=ube (int n) {
out.println(“</tr>”); return n*computeSquare(n);
}
for (int I = 0 ; I < = 20; I ++) { Private int computeSquareRoot (int n) {
return Math.sqet(n);
If ( i%2 = = 0) { }
Out.println(“<td>” + I + “</td>”);
Out.println(“<td>” + ComputeSquareRoot(i) + “</td>”);
Out.println(“<td>” + ComputeSquare(i) + “</td>”);
Out.println(“<td>” + ComputeCube(i) + “</td>”);
out.println(“</tr>”);
}
}
out.printIn(“</body></html>”);
}
12. Given database cars. Complete the following code in create the table models.
Database: Cars Table: models
Id (INT) Make (Varchar (255)) Model (Varchar (255)) Year (int)
1001 Ferrari 488 GTB 2016

<%@ page import =”java.sql.*”%>


<%
Class.forName(“com.mysql.jdbc.driver”);
Connection conn = DriverManger.getConnection(“jdbc:mysql://localhost:3306/cars”,”admin”,”adminsql”);
Statement stmt=conn.createStatement();
String sql=”CREATE TABLE RESGISTRATION”+”(id INTEGER not NULL,”+”make VARCHAR(255),”+model
VARCHAR(255),”+”year INTEGER,”+”PRIMARY KEY(id))”;
Stmt.executeUPDATE(sql);
Sql=”INSERT INTO models ”+”VALUES (1001,’ferrari’,’488 GTB’,2016)”;
Stmt.executeUpdate(sql);
Sql=”INSERT INTO Registration”+”VALUES (1002,’audi’,’RS GT Spyder’,2014)”;
Stmt.executeUpdate(sql);
Sql=”INSERT INTO Registration”+”VALUES (1003,’Jaguar’,’F-TYPE R’,2017)”;
Stmt.executeUpdate(sql);
Sql=”INSERT INTO Registration”+”VALUES (1004,’Mercedes’,’CLS63 AMG’,2015)”;
Stmt.executeUpdate(sql);
//step 6 clean up env
Rs.close();
Stmt.close();
Conn.close();
%>

13. Calculate the total price and discount when ordering and generate the output as shown.
Quantity Price (RM) Discount Order Received! Thank You!
1-5 2.00 0% Total: RM 12.60
6-10 1.80 10% You have ordered 7 chocolate sundaes

<html><body>
<h3> ORder Recieved! Thank you!</h3>
<%
String quantity= request.getParameter("quantity");
double total=0;
double price;
if(quantity!=null){ int q=Integer.parseInt(quantity);
if(q<5)
price=2.00;
else if(6<=q&&q<=10)
price=1.80;
else if(11<=q&&q<=20)
price=1.60;
else
price=1.20;
total=price*q;
} %>
<h3>Total:RM <%= total%></h3>
<h3> you have ordered <%=quantity%> chocolate sundaes.</h3>
</body></html>