You are on page 1of 4

Assignment No.

02
Total Marks: 20
Semester: Fall 2022
Compiler Construction CS506 Due Date: 25th Jan, 2023

Instructions:

Please read the following instructions carefully before submitting assignment. It should be clear that
your assignment will not get any credit if:

 The assignment is submitted after due date.


 The submitted assignment does not open or file is corrupt.
 Assignment is copied (partial or full) from any source (websites, forums, students, etc.)
Strict action will be taken in this regard.
 The assignment format is other than doc/docx.

Objectives:

The objective of this assignment is to provide hands-on experience of Java Programming concepts
including:

 Servlet Basics
 Process Request
 Member Functions and main function

For any assignment related query, contact at CS506@vu.edu.pk

Covered Lectures: Assignment no.2 is from 23-35 lectures.

Software (s) Used to develop Assignment


Note: Use any tool for coding but copy/past that code in word format (doc, docx) file.
Submit word format (doc, docx) file of complete code including both:
1. FirstServlet ProcessRequest() method
2. SecondServelet ProcessRequest() method
(No multiple or zip files allowed)
Problem Statement: Marks 20
 
Write the code for two Java Servlets (processRequest() methods only) in such a way that user enter
his/her “bill_amount” and submit the form to the FirstServlet. FirstServlet calculates "tax" on that
amount and forwards the request (using Request Dispatcher method) to SecondServlet that displays
the amount and calculated tax.

Formula to calculate Tax: bill_amount * 10 / 100

URL pattern of second Servlet: /secondservlet


 

Note: No need to write complete code, just write the code in processRequest() method of each Servlet,
as shown below.

Solution Sample:

FirstServlet processRequest() method:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

Solution:
protected void processRequest(HttpServletRequest request,
response HttpServletResponse)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
// get the salary text value from the HTML form
String bamot = request.getParameter("bamot");
// convert to integer.
int sal = Integer.parseInt(bamot);
// calculation of 15% tax
int tax = (int)(sal * 0.10);
// convert tax to string
String taxValue = tax + "";
The file was created by Vu Expert | Subscribe YouTube Channel: Vu Expert |

// request object can store values in key-value form, later


// can be obtained using the getAttribute() method.
request.setAttribute("tax", taxValue);
// get the servletContext object
ServletContext sContext = getServletContext();
// get the request dispatcher object
RequestDispatcher rd = sContext.getRequestDispatcher("/secondservlet");
// call the request dispatcher's redirect method
rd.forward(request, response);
}
}

SecondServlet processRequest() method:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

Solution:-
protected void processRequest(HttpServletRequest request,
response HttpServletResponse)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String biamot = request.getParameter("bamot");
String tax = (String)request.getAttribute("tax");
// generate HTML tags using PrintWriter
out.println("<html>");
The file was created by Vu Expert | Subscribe YouTube Channel: Vu Expert |
out.println("<head>");
out.println("<title>SecondServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3> Salary " + biamot+ "</h3>");
out.println("<h3> Tax " + tax+ "</h3>");
out.println("</body>");
out.println("</html>");
out.close();
}
}

BEST OF LUCK

You might also like