After Data validation. We are going to create another class under the utility package. So what is the ServletUtility class and why we are using this in Java web application development? Let’s understand the use of this class.
ServletUtility Class
Again, Its custom class that will help us to during the Java web application development. We are going to write some common methods to manage the Request and Response of the servlets or any other activities related to Java servlets.
Here are some methods, that we are going to discuss. You can add more again its depend on the requirement.
forward(page, request, response)
In this method, we are using getRequestDispatcher(page); it will help to forward a page.
public static void forward(String page, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RequestDispatcher rd = request.getRequestDispatcher(page); System.out.println(page); rd.forward(request, response); }
redirect(page, request, response)
In this method, we are using sendRedirect(page); it will help to redirect a page.
public static void redirect(String page, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.sendRedirect(page); }
Source code of ServletUtility class
All the method of this class is just used to manage the resources of related to servlet. Like
- Send request one page to another page.
- Send Error message.
- To forward page.
- To redirect a page.
- Get and Set the list.
package com.javawebapp.utility; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * This class provides utility operation for Servlet container like forward, * redirect, handle generic exception=same time exception, manage success and * error message, manage default Bean and List, manage pagination parameters * * @author Navigable Set * @version 1.0 * @Copyright (c) Navigable Set */ public class ServletUtility { static String Error = "Invalid data"; static String SucessMessage = "Done"; public static void forward(String page, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RequestDispatcher rd = request.getRequestDispatcher(page); System.out.println(page); rd.forward(request, response); } public static void redirect(String page, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.sendRedirect(page); } public static void handleException(Exception e, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setAttribute("exception", e); ServletUtility.forward("Error Message", request, response); e.printStackTrace(); } public static String getErrorMessage(String property, HttpServletRequest request) { String val = (String) request.getAttribute(property); if (val == null) { return ""; } else { return val; } } public static String getMessage(String property, HttpServletRequest request) { String val = (String) request.getAttribute(property); if (val == null) { return ""; } else { return val; } } public static void setErrorMessage(String msg, HttpServletRequest request) { request.setAttribute(Error, msg); } public static String getErrorMessage(HttpServletRequest request) { String val = (String) request.getAttribute(Error); if (val == null) { return ""; } else { return val; } } public static void setSuccessMessage(String msg, HttpServletRequest request) { request.setAttribute(SucessMessage, msg); } public static String getSuccessMessage(HttpServletRequest request) { String val = (String) request.getAttribute(SucessMessage); if (val == null) { return ""; } else { return val; } } public static String getParameter(String property, HttpServletRequest request) { String val = (String) request.getParameter(property); if (val == null) { return ""; } else { return val; } } public static void setList(List list, HttpServletRequest request) { request.setAttribute("list", list); } public static List getList(HttpServletRequest request) { return (List) request.getAttribute("list"); } /** * Sets Page Number for List pages * * @param pageNo * @param request */ public static void setPageNo(int pageNo, HttpServletRequest request) { request.setAttribute("pageNo", pageNo); } public static int getPageNo(HttpServletRequest request) { return (Integer) request.getAttribute("pageNo"); } public static void setSize(int size, HttpServletRequest request) { request.setAttribute("size", size); } public static int getSize(HttpServletRequest request) { return (Integer) request.getAttribute("size"); } public static void setPageSize(int pageSize, HttpServletRequest request) { request.setAttribute("pageSize", pageSize); } public static int getPageSize(HttpServletRequest request) { return (Integer) request.getAttribute("pageSize"); } public static void setOpration(String msg, HttpServletRequest request) { request.setAttribute("Opration", msg); } public static String getOpration(HttpServletRequest request) { String val = (String) request.getAttribute("Opration"); if (val == null) { return ""; } else { return val; } } }