0% found this document useful (0 votes)
278 views2 pages

Cs 506 Ass 2 Sol

The document provides code solutions for two servlets. The first servlet calculates 10% tax on a salary input and stores it in a request attribute. It then forwards the request to the second servlet. The second servlet retrieves the salary and tax values and generates an HTML page displaying them.

Uploaded by

Rizwana Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
278 views2 pages

Cs 506 Ass 2 Sol

The document provides code solutions for two servlets. The first servlet calculates 10% tax on a salary input and stores it in a request attribute. It then forwards the request to the second servlet. The second servlet retrieves the salary and tax values and generates an HTML page displaying them.

Uploaded by

Rizwana Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Cs506 assignment 2 Solution

Student id : Bc190204734

Solution of First and second servlet:

First Servlet:
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{ response.setContentType("text/html;charset=UTF-8"); try

(PrintWriter out = response.getWriter()) {

// getting value of salary text filed of the HTML form


String bamot = request.getParameter("bamot");
// converting it to the integer. int sal =

Integer.parseInt(bamot);

// calculating 15% tax int tax = (int)

(sal * 0.10);

// converting tax into string


String taxValue = tax + "";
// request object can store values in key-value form, later it // can be retrieved

by using getAttribute() method request.setAttribute("tax", taxValue);

// getting object of servletContext


ServletContext sContext = getServletContext();
// getting object of request dispatcher
RequestDispatcher rd = sContext.getRequestDispatcher("/secondservlet");
// calling forward method of request dispatcher rd.forward(request, response);

}
}
Second Servlet:

protected void processRequest(HttpServletRequest request,


HttpServletResponse response) 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"); // generating

HTML tags using PrintWriter out.println("<html>");

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();

}
}

You might also like