Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ideiudicibus/0642a1ca6dc40d90dc9568690b638114 to your computer and use it in GitHub Desktop.
Save ideiudicibus/0642a1ca6dc40d90dc9568690b638114 to your computer and use it in GitHub Desktop.
Servlet Request String Handling
//Source:http://www.exampledepot.com/egs/javax.servlet/GetReqUrl.html
// http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=789
// The most convenient method for reconstructing the original URL is to use
// ServletRequest.getRequestURL(), which returns all but the query string. Adding the
// query string reconstructs an equivalent of the original requesting URL:
// http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=789
public static String getUrl(HttpServletRequest req) {
String reqUrl = req.getRequestURL().toString();
String queryString = req.getQueryString(); // d=789
if (queryString != null) {
reqUrl += "?"+queryString;
}
return reqUrl;
}
// If the hostname is not needed, ServletRequest.getRequestURI() should be used:
// /mywebapp/servlet/MyServlet/a/b;c=123?d=789
public static String getUrl2(HttpServletRequest req) {
String reqUri = req.getRequestURI().toString();
String queryString = req.getQueryString(); // d=789
if (queryString != null) {
reqUri += "?"+queryString;
}
return reqUri;
}
// The original URL can also be reconstructed from more basic components available
// to the servlet:
// http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789
public static String getUrl3(HttpServletRequest req) {
String scheme = req.getScheme(); // http
String serverName = req.getServerName(); // hostname.com
int serverPort = req.getServerPort(); // 80
String contextPath = req.getContextPath(); // /mywebapp
String servletPath = req.getServletPath(); // /servlet/MyServlet
String pathInfo = req.getPathInfo(); // /a/b;c=123
String queryString = req.getQueryString(); // d=789
// Reconstruct original requesting URL
String url = scheme + "://" + serverName + ":" + serverPort
+ contextPath
+ servletPath;
if (pathInfo != null) {
url += pathInfo;
}
if (queryString != null) {
url += "?"+queryString;
}
return url;
}
// Getting a Request Parameter in a Servlet
In a GET request, the request parameters are taken from the query string (the data
following the question mark on the URL). For example,
the URL http://hostname.com?p1=v1&p2=v2 contains two request
parameters – – p1 and p2. In a POST request, the
request parameters are taken from both query string and the posted data which is
encoded in the body of the request. This example demonstrates how to get the
value of a request parameter in either a GET or POST request.
// See also The Quintessential Servlet
// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGetOrPost(req, resp);
}
// This method is called by the servlet container to process a POST request.
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGetOrPost(req, resp);
}
// This method handles both GET and POST requests.
private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Get the value of a request parameter; the name is case-sensitive
String name = "param";
String value = req.getParameter(name);
if (value == null) {
// The request parameter 'param' was not present in the query string
// e.g. http://hostname.com?a=b
} else if ("".equals(value)) {
// The request parameter 'param' was present in the query string but has no value
// e.g. http://hostname.com?param=&a=b
}
// The following generates a page showing all the request parameters
PrintWriter out = resp.getWriter();
resp.setContentType("text/plain");
// Get the values of all request parameters
Enumeration enum = req.getParameterNames();
for (; enum.hasMoreElements(); ) {
// Get the name of the request parameter
name = (String)enum.nextElement();
out.println(name);
// Get the value of the request parameter
value = req.getParameter(name);
// If the request parameter can appear more than once in the query string, get all values
String[] values = req.getParameterValues(name);
for (int i=0; i<values.length; i++) {
out.println(" "+values[i]);
}
}
out.close();
}
// The Quintessential Servlet
// This example implements a servlet that handles GET requests.
package com.mycompany;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
// This method is called by the servlet container just before this servlet
// is put into service. Note that it is possible that more than one
// instance of this servlet can be created in the same VM.
public void init() throws ServletException {
// Initialization code...
// See Getting and Setting Initialization Parameters for a Servlet
}
// This method is called by the servlet container to process a GET request.
// There may be many threads calling this method simultaneously.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
out.println("<html><head><title>A Simple Servlet</title></head><body>");
out.println("Today is "+(new java.util.Date()));
out.println("</body></html>");
out.close();
}
// This method is called by the servlet container just after this servlet
// is removed from service.
public void destroy() {
// Shutdown code...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment