@WebServlet Url Embedding Example

In this tutorial you will learn how can you embed the url of another web component in servlet.

@WebServlet Url Embedding Example

In this tutorial you will learn how can you embed the url of another web component in servlet.

@WebServlet Url Embedding Example

Servlet Url Embedding Example

In this tutorial you will learn how can you embed the url of another web component in servlet.

Embedding URL is a process of invoking another web resource by a web component indirectly. Using this process content of web component is responded for a request made by client.

In the example given below I tried to demonstrate you about how to embed an url. Here the url which I construct here is a sub part of constructing the url. You can construct a complete url (according to your uses) by using some of the methods provided to you by the following interfaces :

  1. javax.servlet.ServletRequest
    • getScheme() : eg. http, https, ftp.
    • getServerName() : returns the hostname of server.
    • getServerPort() : returns the port number.
  2. javax.servlet.http.HttpServletRequest
    • getContextPath() : portion of the request URI is given back which specifies the context of your request.
    • getServletPath() : gives URL which calls the servlet.
    • getPathInfo() : gives the additional path information, attached with the URL.
    • getQueryString() : gives the query string hold by the request URL subsequent to the path.

Example

UrlRequestExample.java

package roseindia.urlExample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name="UrlRequestExample", urlPatterns= {"/UrlRequestExample"})
public class UrlRequestExample extends HttpServlet
{
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String contextPath= request.getContextPath();
out.println("To view another web component");
out.println("<a href="+contextPath+"/UrlResponseExample>Click</a>");
out.println("here.");
System.out.println(request.getPathInfo());
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}

UrlResponseExample.java

package roseindia.urlExample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="UrlResponseExample", urlPatterns={"/UrlResponseExample"})
public class UrlResponseExample extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out= response.getWriter();
out.println("<h3>This servlet is called indirectly through the url requesting</h3>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

Output

When you will execute the servlet "UrlRequestExample.java" you will get the output as :

After execution of the servlet "UrlRequestExample.java" successfully and when you will click on the hyperlink "Click" you will be redirected to the another servlet which contains the some content as :

Download Source Code