Home » Archive

Articles tagged with: servlet

Java Codes »

[12 Feb 2009 | No Comment | 149 views]

These are the four methods that a servlet can use to get information about its server:
public String ServletRequest.getServerName()
public String ServletRequest.getServerPort()
public String ServletContext.getServerInfo()
public String ServletRequest.getAttributes(String name)
The sample code below uses the above function and print the server information to client.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoServerSnoop extends GenericServlet{

public void service(ServletRequest req , ServletResponse res)
throws ServletException,IOException{

res.setContentType(“text/plain”);
PrintWriter out= res.getWriter();
out.println(“req.getServerName()” + req.getServerName());
out.println(“req.getServerPort()” + req.getServerPort());

out.println(“ServletContext().getServerInfo()” +
getServletContext().getServerInfo());

out.println(“getServerInfo() name:” +
getServerInfoName(getServletContext().getServerInfo()));

out.println(“getServerInfo() version:” +
getServerInfoVersion(getServletContext().getServerInfo()));

out.println(“getServerContext().getAttribute(\”attribute\”)” +
getServletContext().getAttribute(“attribute”));
}
private String getServerInfoName(String serverInfo){

int slash = serverInfo.indexOf(‘/’);
if(slash==-1)
return serverInfo;

Java Codes »

[12 Feb 2009 | No Comment | 176 views]

A servlet can use getRemoteAddr() and getRemoteHost() to retrieve the IP Address and host of the client machine respectively:
public String ServletRequest.getRemoteAddr()
public Stirng ServletRequest.getRemoteHost()
Using these functions access to specific client host can be restricted. The sample code below checks the client specification at the server and send the relevant message to the client.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoExportRestriction extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{

res.setContentType(“text/plain”);
PrintWriter out= res.getWriter();

//Getting the client’s hostname
String remoteHost = req.getRemoteHost();

//See if the client is allowed
if(!isHostAllowed(remoteHost)){
out.println(“Access <BLINK>ACCESS DENIED </BLINK>”);
} else{
out.println(“access granted”);
}
}
private boolean isHostAllowed(String host) {
return(host.endsWith(“.com”))||
(host.indexOf(‘.’)==-1);//no domain , assume OK
}
}

Java Codes »

[12 Feb 2009 | No Comment | 166 views]

Servers like JRun and other provide separate logs where all your print requests print their data. For example requests to System.out. And System.err goes to different log files. If you want to maintain logs separately for each module then you can create a static class called “LogWriter” and call the method inside the try catch loop as follows.
try
{
// write your code here
}
catch(Exception e)
{
LogWriter.print(“Debug : error occurred at function ”)
}

Java Codes »

[12 Feb 2009 | No Comment | 137 views]

For example, you have two servlets (Servlet1 and Servlet2).
In Servlet1, you have a method called getString().
In Servlet2, use
ServletContext context = getServletContext();
to get servlet context of Servlet2. Then use
Servlet1 servlet = (Servlet1)context.getServlet(“Servlet1″);
to get the Servlet1 servlet object.
Now, you can Call Servlet1 method from Servlet2 with
servlet.getString();

Java Codes »

[12 Feb 2009 | No Comment | 191 views]

This J2EE tip demostrates chaining method in servlets. Servlet Chaining means the output of one servlet act as a input to another servlet. Servlet Aliasing allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet alias. The output from first Servlet is sent as input to other Servlet and so on. The Output from the last Servlet is sent back to the browser. The entire process is called Servlet Chaining.
// FirstServlet
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet {
String name ;
ServletConfig config;

Java Codes »

[12 Feb 2009 | No Comment | 115 views]

This J2EE tip demonstrates the difference in use of between encodeURL and sendRiderct. Further it also demonstrates the neccesiaty for encodeURL. In order for the URL’s to be in universal specificied format it must be specified in the return type of encodeURL which is then passed on to the sendRedirect.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class EncodeURL extends HttpServlet {
public void doGet(HttpServletRequest request ,
HttpServletResponse response)
throws ServletException , IOException {
response.setContentType(“text/plain”);
PrintWriter out = response.getWriter();
String encode = response.encodeURL(“http://www.google.com”);
response.sendRedirect(encode);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException , IOException {
doGet(request , response);
}
}