Home » Archive

Articles tagged with: servlet

Java Codes »

[6 Mar 2009 | No Comment | 191 views]

This example shows how to get the parameters sent in a http request to a servlet. In this example we use the method service() which will process requests of type GET as well as requests of type POST.To get the parameter names the method getParameterNames() is called on the request object. The method returns an Enumeration which we use to loop through the parameter names, and for each name the method getParameter() is called on the request object to get the value of that parameter.
import java.io.*;import java.util.Enumeration;import javax.servlet.*;import javax.servlet.http.*;/** * Example …

Java Codes »

[6 Mar 2009 | No Comment | 51 views]

This code example shows how to set and get variables stored in a session. To get a reference to the session object we call the getSession() method of the HttpServletRequest object.We send the argument true to the method to tell it to create the session if it doesn’t exist.Then we try to get the session variable ‘VisitCounter’ from the session by calling the method getAttribute(). It’s supposed to be stored as an Integer object, so we cast it to an Integer directly.If the parameter is null, we create a new …

Java Codes »

[6 Mar 2009 | No Comment | 44 views]

This example shows how to forward a http call from a servlet to another page in the same web application.To do a forward we need to get an instance of RequestDispatcher by calling the getRequestDispatcher() method on the HttpServletRequest object.The page to which the call will be forwarded is sent to the method as parameter. Finally the forward() method of the RequestDispatcher is called with both the request and response objects as parameters.
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/** * Example Servlet * @au*#116;*#104;o*#114; *#119;*#119;w*#46;javadb.*#99;*#111;*#109; */public class ExampleServlet extends HttpServlet {        /** Processes requests for both HTTP …

Java Codes »

[6 Mar 2009 | No Comment | 64 views]

To redirect a call from a servlet to some other url, use the sendRedirect() method of the HttpServletResponse object that is passed to the service() method.The sendRedirect() method takes the new destination url as a string parameter.
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/** * Example Servlet * *#64;*#97;*#117;*#116;*#104;*#111;*#114; *#119;w*#119;.*#106;a*#118;*#97;d*#98;.*#99;*#111;*#109; */public class ExampleServlet extends HttpServlet {        /**      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.     * @param request servlet request     * @param response servlet response     */    protected void service(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {        //Redirect call to another url        response.sendRedirect(“http://www.java.com”);    }    }

Java Codes »

[13 Feb 2009 | No Comment | 194 views]

Some of the way, we can make fancy text. There are lot of good way but these are few of them. If you really want to make it fancy then please do some R&D with these API’s used in this code. Here is a small code:

package addingfontinstyle;

import javafx.scene.effect.DropShadow;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.effect.light.DistantLight;
import javafx.scene.effect.light.SpotLight;
import javafx.scene.effect.Lighting;
import javafx.scene.paint.Color;

Java Codes »

[13 Feb 2009 | One Comment | 1,497 views]

OK, nothing to laugh. I know my animation sense is little poor. But here I tried to move a ship, in the way they show in movies -D. Nothing like that, I have tried to give a sinusoidal movement of a ship. In the comment section, you can see there is a sea image as well. Animation was looking little ugly with sea, so I removed it . But point to note, you can give any animation to a image based on any mathematical methods. And if you have …

Java Codes »

[13 Feb 2009 | No Comment | 235 views]

This J2EE program demonstrates a method of handling status code errors in web.xml (Tested in weblogic 7.0). This is important since normal users are not familier with statuscode it. Therefore, the servlet can use the status codes to indicate the status of the response. This method does not trigger the container to generate an error page. It just sends the status code to the browser.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ServletExceptionHandling extends HttpServlet {
  public void doGet(HttpServletRequest request, 
      HttpServletResponse response) 
      throws ServletException , IOException {
    response.setContentType(“text/plain”);
    
    PrintWriter out = response.getWriter();
    int a = request.getContentLength();
    // The default content Length is -1
    if(a==-1) {
      response.sendError(response.SC_LENGTH_REQUIRED,
         “Content Length Required”);
    }
  }
}
Here is the web.xml for the servlet developer can specify this code anywhere in where in web.xml but should be outside servlet & servlet-mapping tags.
<error-page>
<error-code> 411 …

Java Codes »

[12 Feb 2009 | One Comment | 2,281 views]
Java Servlet – How to get/set values from servlet page to session object

Applications like UserManagement, Shopping Cart etc require to store values in cache so that when a user returns his information is restored from cache.
There are three ways to implement sessions in Java Servlets:
* Cookies: HTTP Cookies to store information. Methods response.addCookie(cookie) will add cookie and request.getCookies() will return value of cookies.
* URL Rewriting: Append data on the end of each URL that identifies the session like http://localhost/yourservlet?session-id=value.
* Hidden form fields: HTML forms can have

Java Codes »

[12 Feb 2009 | No Comment | 178 views]

This Java tip illustrates a method of getting the client’s address in a Servlet. In an embodiment developer may use this tip in their client server applications for knowing at the server end the address of the client which made the request.
// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

// Get client’s IP address
String addr = req.getRemoteAddr(); // 123.123.123.123

// Get client’s hostname
String host = req.getRemoteHost(); // hostname.com
}

Java Codes »

[12 Feb 2009 | No Comment | 157 views]

The method getRemoteUser() of the HttpServletRequest gives the username of the client. With the remote user’s name, a servlet can save information about each client. Over the long term, it can remember each individual’s preferences. For the short term, it can remember the series of pages, viewed by the client and use them to add a sense of state to a stateless HTTP protocol.
A simple servlet that uses getRemoteUser() can greet its clients by name and remember when each last logged in as shown in the example below:
import java.io.*;
import java.sql.Date;
import …