Lab Task1 Awd
Lab Task1 Awd
Learning Outcomes:
Questions:
1.
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
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.*;
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
<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 {
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.*;
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