In this tutorial you will learn that how to get the client information from request object
Servlet Get Client Information
You can get the client information from javax.servlet.http.HttpServletRequest, such as address, method, protocol, language code, country code etc. The getLocale() method is return the browser preferred locale. This can be used for Internationalization (Localization) of an application.
You can also get all the query string from the request url by calling the request.getQueryString()
Following are some methods than can be
Locale locale = request.getLocale(); String localeName = request.getLocalName(); String localeAddress = request.getLocalAddr(); String protocol = request.getProtocol(); String method = request.getMethod(); String queryString = request.getQueryString(); String countryName = locale.getCountry(); String language = locale.getLanguage(); String param = request.getParameter("paramName"); Map<String, String> paramMap = request.getParameterMap(); Enumerationparams = request.getParameterNames();
A sample Servlet class is given below
ShowClientInfo.java
package devmanuals.com.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Locale; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShowClientInfo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/html"); Locale locale = request.getLocale(); String localeName = request.getLocalName(); String localeAddress = request.getLocalAddr(); String protocol = request.getProtocol(); String method = request.getMethod(); String queryString = request.getQueryString(); String countryName = locale.getCountry(); String language = locale.getLanguage(); //String param = request.getParameter(""); Map<String, String> paramMap = request.getParameterMap(); Enumeration<String> params = request.getParameterNames(); out.println("localeName- " + localeName); out.println("<br>localeAddress- " + localeAddress); out.println("<br>protocol- " + protocol); out.println("<br>method- " + method); out.println("<br>queryString- " + queryString); out.println("<br>Country- " + countryName); out.println("<Br>Language- " + language); while(params.hasMoreElements()){ out.print("<BR>Parameter- "+params.nextElement()); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Mapping for the above servlet in web.xml will be
<servlet> <servlet-name>ShowClientInfo</servlet-name> <servlet-class>devmanuals.com.servlet.ShowClientInfo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowClientInfo</servlet-name> <url-pattern>/check-locale.html</url-pattern> </servlet-mapping>
[ 0 ] Comments