Cant use cookies in servlet

I can't use cookie in my Servlet upon Tomcat.
When I reach the statement "cookies.length" in code a NullPointerException is generated.
Logfile according to:
2003-05-22 02:51:55 StandardContext[pk]: Reloading this Context is completed
2003-05-22 02:51:55 HTMLManager: list: Listing contexts for virtual host 'localhost'
2003-05-22 02:52:03 StandardWrapperValve[pk]: Servlet.service() for servlet pk threw exception
java.lang.NullPointerException
at calender.pk.doGet(pk.java:34)
I guess my problem might be that cookies is not enabled but I can't solve it.
My web.xml is :
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>pk</servlet-name>
<servlet-class>calender.pk</servlet-class>
<init-param>
<param-name>Cookies</param-name>
<param-value>True</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>pk</servlet-name>
<url-pattern>/pk</url-pattern>
</servlet-mapping>
</web-app>

Here is my code. The problem occurs when deplying onto tomcat4.1.10. When running servlet from DevelopmentEnvironment(JBuilder5) everything works fine.
package calender;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class pk extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
private static final String STARTPAGE = "startpage";
private static final String LOGIN = "login";
private static final String CHECK = "check";
private static final String USERNAME = "username";
private static final String WELCOME = "welcome";
private String welcome = "false";
int responseCode;
public void init() throws ServletException {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Debugger.debug("get start");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String function = request.getParameter("function");
Cookie cookies[] = request.getCookies();
String username = "";
Actionhandler handler = new Actionhandler();
for(int i=0;i<cookies.length;i++){
Debugger.debug("cookieloop i get");
if(cookies.getName().equals("welcome")){
welcome = cookies[i].getValue();
}else if(cookies[i].getName().equals("username")){
username =cookies[i].getValue();
if(welcome.equals("true") && username != null && username != ""){
if(function != null){
if(function.equals("add_worker_to_happening") ||
function.equals("remove_all_workers_from_happening")||
function.equals("change_to_status_ready")){
handler.setHc_id(Integer.parseInt(request.getParameter("hc_id")));
}else if(function.equals("remove_worker_from_happening")){
handler.setWh_id(Integer.parseInt(request.getParameter("wh_id")));
}else if(function.equals("remove_happening")){
handler.setH_id(Integer.parseInt(request.getParameter("h_id")));
responseCode = handler.getPage(request, response, username, function);
}else{
responseCode = handler.getPage(request, response, username, STARTPAGE);
}else{
responseCode = handler.getPage(request, response, username, LOGIN);
if(handler.getHTML() == null){
handler.getResponse().getWriter().write("no output");
else handler.getResponse().getWriter().write(handler.getHTML());

Similar Messages

  • Using cookies in servlet

    Am setting up a cookie in my servlet as follows:
    String sessionID = (String) session.getId();
    Cookie info = new Cookie("userData", sessionID);
    response.addCookie(info); response.sendRedirect(response.encodeRedirectURL("http4://localhost:8084/root/welcome.jsp"));
    When i direct the response to the URI shown, the welcome.jsp does not see the cookie. This is the codes i have used in the jsp:
    <%
    String cookieName = "userData";
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
    for(int i=0; i<cookies.length; i++) {
    Cookie cookie = cookies;
    if (cookieName.equals(cookie.getName())) {
    String mycookie = cookie.getName();
    //doSomethingWith(cookie.getValue());
    %>
    The servlet is called by a https page and i wnat to direct the cookie to http page. Where am i going wrong?
    Thanks in advance.
    M.

    Cookies are domainspecific. Likely you've redirected
    the page to another domain.hi BalusC,
    I don't think so coz am redirecting to the same domain. If i use RequestDispatcher rd = request.getRequestDispatcher("/welcome.jsp"); rd.forward(request, response);
    it works fine but i would like to sent the servlet to another protocol, http. Initially was https.

  • Is there a way of not using cookies or "customized" url's? (servlets+jsp)

    Hi there.
    I'm wondering if there's an already implemented way of establishing a session without using cookies or url withs session id variables. Maybe it's possible uging IP to know wich computer is connected along with something else.

    Actually, I don't hace any requierements to use only IP but I'm learning to develop web apps in general and this is a doubt I've been having for a while.
    I guess IP+browser+screen res.+something else could work.
    Someone would possibly want to use this so the browser user, even with some skills wouldn't know he's being tracked.
    Maybe I have this doubt because I always hear about all the tracking done by companys for several purposes. But I'm not sure.
    Anyway, all this started when I started reading about sessions and users loging in.
    Now. My real doubt is: What kind of tracking should be used and how should it be done?

  • How to use Cookies in Web DynPro?

    Hello,
    Is there a way to use Cookies in Web DynPro like a regular Servlet uses in it's response Object?
    If yes, I could use a code example...
    Roy

    Hi Roy,
    1. This will create a Request Objec containing all the client's request just like in a regular Servlet or does it have special considerations I should worry about?
    A: If you have portal and webdynpro components, both have to use the same runtime. If you are using only webdynpro components then you can use the "Task" class. But it is a non-standard API.
    Task.getCurrentTask().getWebContextAdapter().getHttpServletRequest()
    2. Where do you recommend putting this statement? I assume at the wdDoInit() method right?
    A: Don't put this statement in the doInit(), if you are handling some event within the component and refreshes the view, your doInit() will not get executed.
    3. Is there a Response object I can create as well?
    A:
    HttpServletResponse response =((com.sap.tc.webdynpro.services.sal.adapter.core.IWebContextAdapter) WDWebContextAdapter.getWebContextAdapter()).getHttpServletResponse()
    Regards,
    Santhosh.C

  • How to use cookie in OAF?

    Hi,
    I want to set cookies from OAF controller.
    Any thought or code snippet?
    Abdul Wahid

    Thanks keerthi,
    Late reply.I guess, the article is about how oaf foundations is maintaining states.
    I did try search, but didn't get any direct solution to use cookies just like its used in servlets.
    The requirement was to maintain session information above the user level sessions. Custom servlet cookies looked fine, but couldn't get way to set those in OAF.
    However, if some body gets similar requirement, the solution found, was, "pageContext.putSessionValueDirect" method.
    Thanks again brother.
    Abdul Wahid

  • Deleting Cookies from Servlet

    hi
    i am working on JDeveloper 11.1.2.3
    i made a servlet filter and it works well but the problem is in the cookies ... i tried to delete the cookies from the servlet but in vain
    i uses this code but it does not work
    cookie.setMaxAge( 0 );
    Thanks

    Hi,
    Check
    http://stackoverflow.com/questions/3466267/problem-removing-cookie-in-servlet
    http://stackoverflow.com/questions/9821919/delete-cookie-from-a-servlet-response

  • Session Cookie in Servlet

    Hi all
    I have a issue please answer me.
    If users disabled cookies.( other than session cookies)
    how i should dynamically switch session cookies.
    and how i can generate session cookies in servlet?
    thanks
    yashvant

    If the user has cookies disabled (session, since persistent ones are rarely used for maintaining session state with a browser), then most containers will attempt 'url-rewriting' and insert the session uid there (in the URL). That should work even if cookies are disabled. In order to access a session, you simply call HttpServletRequest#getSession(). If no session exists, one will be created, else the existing one will be retrieved. The J2EE container will send either a cookie in the response or re-write the URL. You don't have to do anything special.
    - Saish

  • Using cookie with DII

    Here is an example to use cookie with static web service call:
    http://download.oracle.com/docs/cd/B32110_01/web.1013/b28974/j2sewsclient.htm#DAFDHCFA
    could some one give an example using cookie with dynamic invocation interdace to inva=oke web service?
    Thanks

    Dear net pas,
    Hope you are doing good.
    You have raised a very valid issue.
    Please do have a look at the SAP NOTE: 1144722-Global configuration of session cookies and attributes
    Also:
    Protecting Sessions Security
    http://help.sap.com/saphelp_nw70/helpdata/en/44/691ccdce2a3675e10000000a114a6b/frameset.htm
    Here pay special attention at:
    cookies named JSESSIONID (in accordance with the JavaÔ Servlet 2.3 specification) for tracking Web browser sessions.
    For this purpose, make sure that the value of SystemCookiesDataProtection and SystemCookieHTTPProtection properties of the HTTP Provider Service on the server nodes is set to true:
    More info at:
    http://help.sap.com/saphelp_nw70ehp2/Helpdata/EN/44/691ccdce2a3675e10000000a114a6b/content.htm
    Thank you and have a nice day :).
    Kind Regards,
    Hemanth
    SAP AGS

  • Cant see my airplay icon, cant use my remote app on either of my devices

    Hi
    I have the gen 3 apple TV and somehow i can no longer airplay, from my mac book pro, ipad, ipad mini or iphone.  It used to work on all of my devices and now I cant see the icon. 
    When I open my remote app it tells me that I have to turn on Home sharing on my itunes or apple tv.  It is turned on, on all of my devices.
    I have been trying to trouble shoot this issue (all of my devices have the latest updates) and I still cant use my remote or air play
    Any suggestions on how to fix this?

    I was able to fix the issue by unpluging the apple tv, router, and modem all for about 2-3 minutes and then replugging them in

  • How to upload more than 100mb in using com.oreilly.servlet package

    hi all,
    I use com.oreilly.servlet package to upload and i use the following code to upload
    MultipartRequest mr = new MultipartRequest(request,"/tmp/saved",0x10000000);My problem is i can't upload more than 25mb, uploads upto 25mb and shows page cannot displayed err in IE,
    Pls help

    In the webserver there is most likely a configuration option for the maximum size that a request may have. So search through the manual of your particular webserver on how to change that.

  • How do I use cookies to control which part of the timeline to play from?

    Hi there,
    I have created an animation with Adobe Edge. My site uses Concrete5 and I am pulling in the Edge content into an IFRAME on my home page (there my be a better way to do this and I'm open to suggestions). I want the animation to play from the start when someone first visits the site, but if during their browser session they navigate back to the home page, I want the animation to only play a shorter segment of frames near the end.
    My question is, how do I use cookies to acheive this? I'm new to javascript/jquery.
    I've included the following code on compositionReady, (found in another post on this forum) but don't have a clue how to continue...
    // insert code to be run when the composition is fully loaded here yepnope(   {     nope:[       '/js/jquery.cookie.js'     ],   complete: init   } ); function init() { //create your cookie's initial values here } 
    My temp site is here - http://79.170.40.43/nutcrackerdesign.co.uk/
    On revisting the homepage, I only want to play from when the green 'How can we help?' button drops in.
    Many thanks!
    Russ

    Hi, Russ-
    I found this article, which seemed really helpful in describing how cookies work in JavaScript:
    http://www.quirksmode.org/js/cookies.html
    Remember that JS works just fine within Animate, so on your compositionReady, you can read your cookie and then set the play based on that.  You should probably uncheck the autoplay for your Stage and control the play of your Stage from the compositionReady.
    Good luck!
    -Elaine

  • How do I transfer an app from itunes my pc to my iphone 4s? I cant use wifi as my chip fried at the last update

    How can I transfer an App purchased on iTunes on my pc to my iPhone 4s. I have tried synching but this does not make the app appear on my iPhone. I cant use wifi as my chip was fried on the latest update.

    When you tried syncing, did you connect the phone, then click on the phone icon near the top left of iTunes, then click on Apps under Settings in the sidebar, then find the app in question and click the Install button beside it before clicking Sync at the bottom right?

  • TS4268 Why i cant use my iMessage in my ipad mini? I use the same id and password in my iphone

    Someone help me with the problem i have. I cant log in into my iMessage in my iPad mini using the same account i am using in my iPhone. I checked the spelling of the password and username they are all correct but still i cant use it on iMessage. Somebody help me. Thanks

    Welcome to the Apple Community.
    In order to change your Apple ID or password for your iCloud account on your iOS device, you need to delete the account from your iOS device first, then add it back using your updated details. (Settings > iCloud, scroll down and hit "Delete Account")
    Just enter your existing details, don't try creating a new account.

  • HT201343 My MBP is Early 2011 but dunno why cant use mirroring with apple TV, the icon cannot be found

    My MBP is Early 2011 but dunno why cant use mirroring with apple TV, the icon cannot be found. So cannot use mirroring.
    I have follow trouble shooting , they recommend to go system preference -> display , thus can find out the mirroring icon if no mean it doesnt support. But according to the web, Early 2011 MBP should be able to use, dunno why.

    Just realise I havent updated my OS to mountain lion and this is the reason why I cannot use mirroring before ^^
    Thanks all

  • I cant use facetime. but my sister can. we share the same account for itunes.but when i go to face itme and click on create a new account a blank screen pops up. what do i do?when i go to make a new facetime account nothing comes up

    i cant use facetime. but my sister can. we share the same account for itunes.but when i go to facetime on my ipod touch and click on create a new account a blank screen pops up and the only thing it says is cancel and account.  what do i do?

    Please follow these directions to delete the Mail "sandbox" folder.
    Back up all data.
    Triple-click the line below to select it:
    ~/Library/Containers/com.apple.mail
    Right-click or control-click the highlighted line and select
    Services ▹ Reveal
    from the contextual menu.* A Finder window should open with a folder named "com.apple.mail" selected. If it does, move the selected folder — not just its contents — to the Desktop. Leave the Finder window open for now.
    Quit and relaunch Mail, and test. If the problem is resolved, you may have to recreate some of your Mail settings. You can then delete the folder you moved and close the Finder window. If you still have the problem, quit Mail again and put the folder back where it was, overwriting the one that may have been created in its place. Post your results.
    Caution: If you change any of the contents of the sandbox, but leave the folder itself in place, Mail may crash or not launch at all. Deleting the whole sandbox will cause it to be rebuilt automatically.
    *If you don't see the contextual menu item, copy the selected text to the Clipboard (command-C). In the Finder, select
    Go ▹ Go to Folder...
    from the menu bar, paste into the box that opens (command-V). You won't see what you pasted because a line break is included. Press return.

Maybe you are looking for