You are on page 1of 12

POLITEKNIK SULTAN IDRIS SHAH

JABATAN TEKNOLOGI MAKLUMAT DAN


KOMUNIKASI
DFW 5013
ADVANCE WEB DEVELOPMENT
LAB TASK 1
NAME MATRIC NO.
MUHAMMAD ABRAR BIN ZULKHURNAIN 17DDT18F1001

LECTURER NAME : MADAM NURUL ZAKIAH BINTI


KASNUN
COURSE : DF5013 – ADVANCE WEB
DEVELOPMENT
DEPARTMENT OF
INFORMATION & COMMUNICATION TECHNOLOGY

DFW5013 – ADVANCED WEB DEVELOPMENT


LABORATORY TASK 1 – Handling the Client Request

Student ID : 17DDT18F1001 Class : SCORE:


CLO2P /10
CLO3P /5
TOTAL /15
Name : Muhammad Abrar bin Zulkhurnain

Utilise Servlet, JSP and web security to develop web application.


CLO : 2 (C4, P4, PLO1, PLO2)
Build a web application by applying server side programming
CLO : 3 technologies. (P4, PLO2)

Learning Outcomes:

Students should be able to:


i. understand HTTP GET method.
ii. understand HTTP POST method.
iii. writing servlet to get parameters values .
iv. use forward request.

Questions:

1.

Figure 1 : Using Get Method

i. Write a servlet program to handle input given by web browser using doGet() method.
By referring HTML code given in Figure 1, use getParameter() method to access
passed information. (CLO2, P4)
ii. You are also need to create web.xml file in the project. (CLO2, P4)
iii. Run the program and display the output. (CLO3, P4)

Index.html
<html>
<body>
<form action="NewServlet" method="Get">
First name: <input type="text" name="first_name">
<br/>
Last name: <input type="text" name="last_name"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

NewServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class

public class NewServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "NAME";
String docType
= "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor = \"#f0f0f0\">\n"
+ "<h1 align = \"center\">" + title + "</h1>\n"
+ "<ul>\n"
+ " <li><b>First name</b>: "
+ request.getParameter("first_name") + "\n"
+ " <li><b>Last name</b>: "
+ request.getParameter("last_name") + "\n"
+ "</ul>\n"
+ "</body>"
+ " </html>"
);
}
}

Output
2.
Figure 2 : Using Post Method

i. Modify the HTML code as shown in Figure 2. Add another input controls in the user
interface such as RadioButton for gender and Checkboxes for favourites of a user.
(CLO2, P4)
ii. Write a servlet program to handle input given by web browser using doPost()
method. By referring to HTML code that you have modified, use getParameter()
method to access passed information. (CLO2, P4)
iii. Modify the web.xml file to link the interface to the servlet. (CLO2, P4)
iv. Run the program and display the output. (CLO3, P4)

Index.html

<html>
<body>
<form action = "ServletAbrar" method = "POST">
First Name: <input type = "text" name = "first_name">
<br />
<br/>
Last Name: <input type = "text" name = "last_name" />
<br/>
<br/>
Gender :<input type="radio" name="gender" value="male" />Male
<input type="radio" name="gender" value="female" />Female
<br/>
<br/>
Favourite Colour:<input type="checkbox" name="colour" value="Red"/> Red
<input type="checkbox" name="colour" value="Green"/> Green
<input type="checkbox" name="colour" value="Blue"/> Blue
<br/>
<br/>
<input type = "submit" value = "Submit" />
</form>
</body>
</html>

Web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>ServletAbrar</servlet-name>
<servlet-class>ServletAbrar</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletAbrar</servlet-name>
<url-pattern>/ServletAbrar</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

ServletAbrar.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class ServletAbrar extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

// Set response content type


response.setContentType("text/html");

PrintWriter out = response.getWriter();


String title = "BIODATA";
String[] colour = request.getParameterValues("colour");
String docType
= "<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";

out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor = \"#f0f0f0\">\n"
+ "<h1 align = \"center\">" + title + "</h1>\n"
+ "<ul>\n"
+ " <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n"
+ " <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n"
+ " <li><b>Gender</b>: "
+ request.getParameter("gender") + "\n"
+ " <li><b>Favourite Colour</b>: "
+ "</body> </html >"
);
for (int i = 0; i < colour.length; i++) {
out.println(colour[i] + "\n");
}
}
}

Output

3. Create two Servlet programs that meet the following scenario: (CLO2, P4)
“Client sends height of a person in meter to a Servlet for converting the height to centimeter
unit. The Servlet forwards the client request to another Servlet using forward() method of
RequestDispatcher interface.”

Run the program and display the user interface for input and result. (CLO3, P4)

Index.html

<html>
<body>
<h1>Meter to Centimeter </h1>
<h3>Enter your height in Meter</h3>
<form action="height" method="Get">
Height in meter: <input type="text" name="height">
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>height</servlet-name>
<servlet-class>height</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>height</servlet-name>
<url-pattern>/height</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>Convert</servlet-name>
<servlet-class>Convert</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Convert</servlet-name>
<url-pattern>/Convert</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Height.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class height extends HttpServlet {

public void service(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("");
RequestDispatcher LM = request.getRequestDispatcher("/Convert");
LM.forward(request, response);
out.println("");
out.close();
}
}

Convert.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Convert extends HttpServlet{


public void service(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out =response.getWriter();

String str1 = request.getParameter("height");


double heightinmeter = Double.parseDouble(str1);

double total = heightinmeter * 100;


out.println("Your height in centimeter : " + total);
out.close();
}
}

Output
PROGRAMMING RUBRIC
Properties Outstanding Exceptional Good Satisfactory Unsatisfactory Weak
5 4 3 2 1 0
Specifications The student’s The student’s The student’s The student’s The student’s code Failed to
(CLO2, P4) code is code is code is code meets most meets partially meets
outstanding and exceptional complete but of the requirements and requirement
[ /5] complete (i.e. complete (i.e. may contain requirements and contains and contains
meets all meets all some contains programming programming
requirements), requirements), programming programming errors. errors.
free from free from errors. errors.
programming programming
errors, and well errors, and is
organized well organized

Readability The code is 100% The code is The code is The code is 40% The code is 20% Failed to
(CLO2, P4) well organized by 80% well 60% well well organized by well organized by organized a
giving a consistent organized by organized by giving not giving not good code and
[ /5] variable naming, giving a giving a fairly consistently well consistently well no comments
consistent consistent organized but organized and organized and may alt all.
formatting with variable may not be may contain contain haphazard
comments naming, consistent in haphazard formatting, variable
consistent formatting, formatting, naming with
formatting with variable naming variable naming comments
comments with comments with comments

Output All program output All program Program output Program output Program output Wrong output
(CLO3, P4) is correct and well output is may not be may be incorrect may be incorrect
formatted. partially and consistently well and/or may not be and same with
[ /5] well formatted. formatted. well formatted. others student

Lecturer This lab contains This project is This is a good The lab The lab Failure to
Comments elements of an excellent lab. lab but may be demonstrates that demonstrates that demonstrate a
problem solving, at a basic level the student only the student only good lab and
creativity, and and/or have partially understands the plagiarism
originality. It is some weak understands the very basic work
among the best. areas. concepts concepts
presented in presented in class.
class.

Overall Total :
/15

You might also like