Need clarification - Rreg Servlets init method and construction

Hi,
My friends asks me 'Is init method of servlet is necessary? Can't we do whatever we want to do in Init method, in the constructor of the servlet? Why is init method is necessary?'
Thanks in advance,
Guhan

byte[] tripleDesKeyData = key.getBytes();
Instead of getting Bytes directly from that Hex string now i am getting in the following way......
byte[] tripleDesKeyData = Encryption.tempHexStringToByteArray(key);
public static byte[] tempHexStringToByteArray(String hexString){
     byte[] bytes = new byte[hexString.length() / 2];
     for (int i = 0; i < bytes.length; i++) {
          bytes[i] = (byte) Integer.parseInt(hexString.substring(2*i, 2*i+2), 16);
     return bytes;
And using that key to decrypt....as of now its working fine now
Why i am posting this is for others to use further....

Similar Messages

  • Iplanet kjs servlet init() method

    In iplanet is the servlet init method called in each KJS engine.
    I have a continous thread running in a servet.This is invoked by a webased hyperlink.Some times ther happens to be 2 threads running and results in false operations.Conflict b/w threads.
    Suspect one thread in each KJS is running.

    This is the incorrect forum for this question. Sun ONE Application Server 7 is a completely new appserver and does not have KJS related technologies or APIs. Please check AppServer 6 forum.
    HTH.
    Paul

  • Invoking servlets init method

    Let's just say I want to write a startup class that initilizes a servlet,
              (just like ServletStartup class). How would I do this without using the
              URLConnection class?
              Thanks
              

    c00ler wrote:
    Hey guys,
    Studying JDBC & Java by O'Reilly. Old book, very old. You'd be better off with the Sun JDBC tutorial. Google for it.
    init() method is not really explained in the book and i'm kind of stuck. As far as i understand it's used to initialise servlet before its execution right? O'Reilly uses it to connect to the database (i'm find with connection part), problem is i don't understand why i can't connect through proccessRequest() method and I can't actually find the init() method within servlet...unless of course i write my own one. There is code offered by O'Reilly:
    Terrible form. More evidence of just how out of date this book is. No one would write a servlet to do this now.
    Book was published quite a few years ago...is there different implementation to this?
    Also for some reason Class.forName().newInstance() throws unidentified error if i use SQLException (even though java.sql.* is there), but works fine with Exception...any ideas???Not unidentified if you look at the javadocs for Class - it throws ClassNotFoundException, among others.
    You don't have to call newInstance() - Class.forName() is sufficient for registering the driver class.
    %

  • Servlet init() method called multiple times

    We have a Servlet-based application which does lengthy initialization.
    We've noticed that when multiple requests arrive,
    but the Servlet's init() method has not returned yet
    for the first request, the application server calls init() multiple times.
    You can even see this with the Fortune sample
    Servlet. Starting the app server and sending 20 or 30
    simultaneous requests causes multiple Fortune Servlet init messages to appear in the kjs log.
    Why is this? We do not implement SingleThreadModel.
    Doesn't the J2EE 1.2 spec say init will be called
    only once?
    How do we prevent it?
    A Startup class which inits our
    Servlet?
    Or a synchronized block within our init method, with some checks of static member variables
    so that the initialization work only happens once?
    Or is there some kregedit magic or deployment descriptor change I could make?
    Again, it's not a SingleThreadModel issue, so don't
    point me there.
    This is iAS 6.0 SP4, on Solaris, by the way.

    Hi
    Yes,you are right. In iAS6.0 SP4 When multiple requests arrive,and the Servlet's init() method has not returned yet for the first request, the application server calls init()multiple times. This problem is resolved in iAS 6.5.
    There is no kregedit magic that will solve this problem.
    Right now the workaround is any of the solution you proposed. Or use iAS6.5. I have tested it with fortune.
    Please download it and check with your application.
    you can find free download at http://developer.iplanet.com/appserver/testdrive/testdrive_65.html
    Thanks

  • Why does SunOne 6.1 call servlet init() method twice on startup

    Hi,
    SunOne6.1 sp 5 is running my servlet's init() method twice when I put load-on-startup in the web-xml file. Is this a known problem and is there a work around as no amount of synchronising seems to work.
    I'm running 2.3.dtd for the default_web.xml file and my entries look like this :-
    <servlet>
    <servlet-name>RelatisLogin</servlet-name>
    <servlet-class>com.MyServlet</servlet-class>
    <load-on-startup>7</load-on-startup>
    </servlet>
    This init method only contains a basic check at the moment :-
    public void init()
    System.out.println("init.....");
    If I take <load-on-startup> away and run the server then call the servlet via the url it only runs init once.
    I've tried synchronizing the block but this doesn't work.
    The synchonization code I used looks like this:-
    public void init(ServletConfig config) throws ServletException
    synchronized( AnotherClass.staticString )
    if ( AnotherClass.staticString.length() > 0 ) return;
    System.out.println( AnotherClass.staticString.length() );
    AnotherClass.staticString = "init has been called";
    try
    int i = 0;
    i = i/i;
    } catch(Exception e)
    e.printStackTrace();
    The declaration of "AnotherClass.staticString" is :-
    public static String staticString = "";
    The output of "System.out.println( AnotherClass.staticString.length() );" is 0 and displayed twice.
    I deliberately cause a divide by zero error to get a stack trace so that i can determine what called init(), the stack trace is printed twice and is always :-
    java.lang.ArithmeticException: / by zero
    at com.MyServlet.init(MyServlet.java:47)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:921)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:813)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3478)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3760)
    at com.iplanet.ias.web.WebModule.start(WebModule.java:251)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1133)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:652)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1133)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:355)
    at org.apache.catalina.startup.Embedded.start(Embedded.java:995)
    at com.iplanet.ias.web.WebContainer.start(WebContainer.java:431)
    at com.iplanet.ias.web.WebContainer.startInstance(WebContainer.java:500)
    at com.iplanet.ias.server.J2EERunner.confPostInit(J2EERunner.java:161)
    It looks like SunOne starts then calls init() and then restarts and calls init() again.
    The same code and same web.xml entries work fine in Tomcat 5, init() only every gets called once.
    Any ideas? Can I post this as a bug?
    Thanks in advance for any help.
    Chris.

    Sorry to have not replied to this thread earlier.....forgot all about it.
    The reason init was being called twice by IPanet (Sun One) is IPanet comes with a default context called "Search" already in server.xml file.
    This context was loading all the classes on my class path and this causing "init" to run. Then my context entry also loaded all the files in the class path.
    Quite why Sun include this "Search" context is beyond me......annoying. If you take it out the the problem goes away.
    Basically if you leave this in then your running to web servers (JVM's) your one and Sun's stupid "Search" one. As they are separate JVM's no amount of synchronisation would ever fix it.

  • Servlet doPost() method and "HTTP Status 404 - /HelloPost" error

    hi all
    i've a problem with doPost(...) method. i use Tomcat4.1 version and in root folder i store Form.html where i use a doGet() method and in WEB-INI>>classes i store Hello.class file for doGet() method. it works normally. but when i write a Form1.html for doPost() method with "<FORM METHOD=POST ACTION="/HelloPost">" line and store as same folder as Form.html and HelloPost.class in WEB-INI>>classes folder. its not working and showing a error
    "HTTP Status 404 - /HelloPost"
    error type =Status error.
    message=/HelloPost
    description =The requested resource (/HelloPost) is not available
    if anyone of u know what the problem is.. then plz inform me... it will help me a lot to learn servlet.
    thnx a lot
    Arif

    Your post is very hard to read. The next time try to be more accurate.
    i store Form.html where i use a doGet() method That makes no sense.
    I think you have a form with method="GET"
    and in WEB-INI>>classes i store Hello.class file for doGet() method. you have a Servlet called Hello which handles doGet(Request, Response) calls and whose class file you've put into WEB-INF/classes
    but when i write a Form1.html for doPost() method with "<FORM METHOD=POST ACTION="/HelloPost">" line and store as same folder as Form.html and HelloPost.class in WEB-INI>>classes folderIt's not enough to put a class file which has the same name as the action of your form into the folder WEB-INF/classes. The class must implement the (Http)Servlet Interface and you have to configure the servlet in the web.xml file.
    From where did you get the working Hello example code ?
    Look at the Chapter 11: Java Servlet Technology of the J2EE 1.4 Tutorial to learn how to write Servlets.

  • Need clarification  in usage of  premise and connection object

    Hi everyone,
       I  am working on a project and for that i have  do move in  for around 30 customers. i did up to move in . I assumed 5 customers from a single appartment and grouped  them. so i  used same connection object and premise for them. After move- in is done  i checked the Equipment numbers  generated for all the 5 in that group. The  address in equipment display(ie03) and also the customer display (XD03)  is overlapped  with the connection objects address. so it is showing same address for all 5 customers. After that i changed the premise but used the same conection object. But again the customer address is overlapped with the given connection object address. can anyone help me how to solve this if i have a scenario  like this :  5 customers from same appartment. how to create the connection obj and premise .
    Thanks,
    Archana

    Archana,
    Please have a look at the IS-U data model. The connection objects relates to the building and the premises to the appartments. The 5 customers/consumers/business partners live in their own premise. On premise level you may detail the address with such as "1st floor last door to the right".
    Devices (meters) are installed technically in a devlice location that can be is linked to premise and connection object. Finally the utility installation holds all billing relevant data while the contract is the link between the technical master data and the business master data. During a move-in the address list of a BP is completed by the address of the connection object (if customized).
    As you can see there are various factors affecting the address of a business partner and a installed device.
    Kind regards,
    Fritz

  • OEM doesn't detect Servlet .init() failure!?!

    I'm trying to move some one-time initialization into a servlet .init() method and I wanted to see what would happen in OEM (Oracle Enterprise Manager) when this servlet's init() method failed (by throwing ServletException). I was hoping to see a Stop Sign or something...but instead OEM thinks everything is fine. Does anyone know how to make OEM aware of the actual state of the Servlets deployed under the OC4J containers it maintains?
    Bob
    package demos;
    import org.apache.struts.action.ActionServlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletContext;
    public class MyActionServlet extends ActionServlet {
    public MyActionServlet() { }
    public void init() throws ServletException {
    super.init();
    ServletContext sc = this.getServletContext();
    sc.setAttribute("AppScopeAttribute",
    "sc.toString()=" + sc.toString());
    sc.setAttribute("MyInitParam",
    this.getInitParameter("MyInitParam"));
    throw new ServletException(
    new Exception("This sucks...that sucks...everything sucks")
    }

    Maybe if I format the source better someone will render an opinion? I'm looking for ways to have the Oracle Enterprise Manager detect and report on .init() problems in my servlets. Here is a sample servlet that does nothing but throw and exception at .init() time. How can I configure OEM to react to and report this condition?
    package demos;
    import org.apache.struts.action.ActionServlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletContext;
    public class MyActionServlet extends ActionServlet {
      public MyActionServlet() { }
      public void init() throws ServletException {
        super.init();
        ServletContext sc = this.getServletContext();
        sc.setAttribute("AppScopeAttribute", "sc.toString()=" + sc.toString());
        sc.setAttribute("MyInitParam", this.getInitParameter("MyInitParam"));
        throw new ServletException( new Exception("This sucks...that sucks...everything sucks"));
    }

  • Configuring OEM to detect servlet.init() failures

    I'm trying to move some one-time initialization into a servlet .init() method and I wanted to see what would happen in OEM (Oracle Enterprise Manager) when this servlet's init() method failed (by throwing ServletException). I wrote a simple Servlet that does nothing but fail when it attempts to initialize. I was hoping to see a Stop Sign or something in OEM when I deloyed it, but instead OEM thinks everything is fine. Does anyone know how to make OEM aware of the actual state of the Servlets deployed under the OC4J containers?
    Bob
    package demos;
    import org.apache.struts.action.ActionServlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletContext;
    public class MyActionServlet extends ActionServlet {
    public MyActionServlet() { }
    public void init() throws ServletException {
    super.init();
    throw new ServletException( new Exception("I failed to init...DO SOMETHING!"));

    Maybe if I format the source better someone will render an opinion? I'm looking for ways to have the Oracle Enterprise Manager detect and report on .init() problems in my servlets. Here is a sample servlet that does nothing but throw and exception at .init() time. How can I configure OEM to react to and report this condition?
    package demos;
    import org.apache.struts.action.ActionServlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletContext;
    public class MyActionServlet extends ActionServlet {
      public MyActionServlet() { }
      public void init() throws ServletException {
        super.init();
        ServletContext sc = this.getServletContext();
        sc.setAttribute("AppScopeAttribute", "sc.toString()=" + sc.toString());
        sc.setAttribute("MyInitParam", this.getInitParameter("MyInitParam"));
        throw new ServletException( new Exception("This sucks...that sucks...everything sucks"));
    }

  • LDAP Context Initialization Problem in a Servlet's Init method

    Hi
    I have a servlet where I am using the InitialDirContext to Initialize context to a LDAP Server. I have created the context as a static variable. I am initializing the context in the init method and am using the same reference in the service method of the servlet. But if the context fails (due to naming exception and time out) in the init method all my requests in the service method will fail. Is there any way to validate the reference before using the same in the service method.
    Also what is the best approach to initialize the context in a servlet. Whether to initialize in the service method or the init method.
    I would appreciate if anyone can answer the above 2 queries.
    Thanks in advace,
    Ashish

    If you want to use threadsafe objects in doPost/doGet there are two common ways to do this.
    First, create the object in doPost/doGet. This may get expensive depending on the object, but it is 'easy' code and works.
    The second option is to create a pool of objects in the servlet's init() method and to just have doPost/doGet get an object from the pool, use it and return it when done. This take a little more work to code, but the overhead of creating objects is almost eliminated. Look at jakarta/apache commons pool for a quick way to do this.
    If you create on object in init() and use it in doPost/doGet, any time you get two request for the same servlet at the same time you will have synchronization issues that will usually cause problems. There is one instance of each Servlet, so all request for that servlet go through the single instance. Anything in doPost/doGet must be threadsafe.

  • Servlet service() method

    Why we open the database connection in the servlet�s doPost() method, instead of the init() method, although opening database connection could be considered a one-time activity ?
    But what happens if you declare the connection as an instance variable, and open it in the init() method, and close it in the destroy() method?
    Can somebody give me more detail examples about this.
    What�s the meaning of :
    The servlet instance can throw a UnavailableException to indicate that it is temporarily or permananetly available, and about temporary or permanent failures?
    Thank You very much.

    A single connection can only be used by one thread at once, so declaring only one connection will make your application very unfriendly when used by multiple users.
    But you are on the right track though, you should not be opening and closing connections for each statement you execute. What you need to look into is connection pooling, and that can be handled by most popular java webservers. If you are using tomcat for example, you can look here on how to set it up and use it:
    http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
    The only thing I have to add to that document is that you need to put your database driver jar in common/lib for this to work.

  • Empty init() methods

    I am currently working on and cleaning up (old) Java servlet code that I inherited from another collegue. In a lot of the servlets, I find empty init() methods like:
    public void init() {
    }As a novice to Java servlets, I have just been reading the First Edition of Jason Hunter's 'Java Servlet Programming' book. There, on page 59 it is stated that every servlet's init() method should call super.init(config) as its first action. I was wondering if I could simply remove the empty init() methods that I encounter in the servlets... If i remove them, the init() method of the superclass will get called... and the behaviour of the superclass init() might be slightly different than an emtpy init() method... isn't it? But I wonder if it will make a big deal for my application...
    Are there any possible side effects to expect here? What could probably happen if remove the empty init() methods and consequently start using the init() method from the superclass?

    OK. Yesterday, I cleaned up all empty init() methods, now it's time for cleaning up all unnecessary init(ServletConfig cfg) methods. In the servlet code that i inherited from a collegue, I find a lot of servlets containing the following:
        public void init(ServletConfig cfg) throws ServletException {
            super.init(cfg);
        }In the meanwhile, i know that we are using Tomcat 5.5 as a container, so I looked into the Tomcat HttpServlet implementation and I found no init(ServletConfig cfg). I then looked at GenericServlet where i found:
        public void init(ServletConfig config) throws ServletException {
            this.config = config;
            this.init();
        }and since the init() method also doesn't do much, i think i can safely remove all
        public void init(ServletConfig cfg) throws ServletException {
            super.init(cfg);
        }methods in our own servlets... since what they do is actually just the same as the init(ServletConfig cfg) method from GenericServlet.
    Can somebody who is more experienced in Java Servlets than me confirm?
    Thanks!
    Bart

  • Calling init() method from "session scope" backing beans (AbstractPageBean)

    hi,
    i prepared a jsp/jsf page named DBmanage, its backing bean's scope is set to "session"
    the page contains - among other components - one radiobuttongroup, the buttons of which i would like to rename at the first time when the user visits the page... (default names are "item 1", "item 2", "item 3")
    i thought that the overridden init() method does the work for me with this simple code:
    import com.sun.webui.jsf.model.Option
    @override
    init() {
    rbg_default_options.setOption( new Option[]{new Option("it1", "RB1"), new Option("it2", "RB2")}
    so, let see how this code works when DBmanage backing bean's scope is "request"
    great! i see a radiobuttongroup with two buttons, named RB1 and RB2
    ok, now i change the scope to "session"
    sh&#@t!!! now i'm gazing at a page with buttons named "item 1", "item 2", "item 3"
    let's place a breakpoint inside the init() method and DEBUG... and my init() code has not run(??), how can this happen? init() method is valid only for request beans?
    pls! could anyone help me?
    ps.: i work with netbeans6.0 & glassfish
    thanks
    Gabor

    it's ok. i found out why it wasn't working.
    my scope was set to "session", but had to be "request" so that i could use the params syntax.
    thanks anyway.

  • Urgent....How can i redirect to my jsp page from servlet in init() method..

    How can i redirect to my jsp page from servlet in init() method..Becoz that servlet is calling while server startsup..so im writing some piece of code in init() method..after that i want to redirect to some jsp page ...is it possible?
    using RequestDispatcher..its not possible..becoz
    RequestDispatcher rd = sc.getRequestDispatcher("goto.jsp");
    rd.foward(req,res);
    Here the request and response are null objects..
    So mi question can frame as how can i get request/response in servlet's init method()..

    Hi guys
    did any one get a solution for this issue. calling a jsp in the startup of the servlet, i mean in the startup servlet. I do have a same req like i need to call a JSP which does some data reterival and calculations and i am putting the results in the cache. so in the jsp there in no output of HTML. when i use the URLConnection i am getting a error as below.
    java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:707)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:612)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:705)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:612)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
    nection.java:519)
    at com.toysrus.fns.alphablox.Startup.callJSP(Unknown Source)
    at com.toysrus.fns.alphablox.Startup.init(Unknown Source)
    at org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
    so plz do let me know how to call a jsp in the start up of a servlet.
    Thanks
    Vidya

  • Write a servlet without doGet() and doPost() methods

    Hi,
    Can we write a servlet without doGet() and doPost() methods ?

    public class MyCoolServlet extends HttpServlet
    public void init(ServletConfig servletConfig) throws ServletException
    public void service(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
    // your code here
    }Just an example on how to do it.
    A servlet is loaded by server (before any request). It will run the init function. Then pick up an request, spawn a thread and put this servlet code into the thread, give it session, request and response objects, then call serivce.
    Remember that init is called before, outside the thread, while service is inside the thread and the last to be called. Nice place to put your code.
    If you need to have some sort of init function to be called first on every request PR USER, then make your own function, and call it first in service method.
    Stuff that are put in init() function might only get runned once in the entire server lifetime. Until restart.

Maybe you are looking for

  • New iTunes 11 - no longer gives count of podcasts

    Hi Since I updated to iTunes 11, I can no longer see the number of podcasts that I have.  There's a number displayed next to podcasts in the top left drop-down menu, but that's the count of unplayed podcasts.  I want to know how many there are in tot

  • Searching for a good book about [Arch Linux]

    Hello friends, I'm here stuck with Windows 8, making a couple of tests about installing Arch Linux in VMware Workstation, if it succeeds, I'll move to Arch completely. My question is: Is there a good book about Arch (installation, maintenance, best p

  • DoS attacks in java(urgent)

    I am an undergraduate student and currently working on network security project based on denial of service attacks in java. I have established a client/server connection and now want to capture all incoming packets at the receiving end(server) and th

  • Documents "Modifed Date" changes just by opening?

    We're seeing that Word, Excel & various other doc's change their "Date Modified" to today's date where they are ONLY opened...not modified. All we're doing is opening and closing, no changes.... Is this a Micro$oft issue or can I change a preference

  • App switcher (command-tab) disappears

    At random intervals my app switcher will misbehave. Sometimes it reuses to show at all when I press command-tab, although the app switch still takes place. Sometimes it will show on screen but there will only be one or two apps in the list even thoug