jbason 0 Newbie Poster

Hi all,

I have been trying to find this out for 4 days straight, doing all types of research and yet I cannot find it, although it seems like one of the most fundamental things I can think of. I am new to jsps and servlets but not new to java, so please be patient with me :)

_____________________________________________________

SCENARIO:

1) I have an index.jsp page with a form with 1 field for the user to fill up.
2) I have a bean which I am trying to fill up from the index page itself.
3) I have a servlet that handles the request

_____________________________________________________

INDEX PAGE:

<%@ page import="com.java2s.*"%>

<html>
<body>
<jsp:useBean id="bean" class="com.java2s.bean" scope="session"/>
<jsp:setProperty name="bean" property="*"/>
<FORM METHOD=POST ACTION="MyServlet">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
<P><INPUT TYPE=SUBMIT>
</FORM>
</body>
</html>

_____________________________________________________

BEAN:

package com.java2s;

public class bean {
    String username;

    public void setUsername( String value )
    {
        username = value;
    }

    public String getUsername() 
    { 
        return username; 
    }
}

_____________________________________________________

SERVLET:

package com.java2s;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyServlet extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {

        HttpSession session = request.getSession(true);
        // Get the bean from the session
        bean myBean = (bean)session.getAttribute("bean");

        // if the bean is not in session instantiate a new bean
        if (myBean == null)
        {
            myBean = new bean();
            myBean.setUsername("FAILED");
        }
        
        request.setAttribute("bean", myBean);
        request.getRequestDispatcher("test.jsp").forward(request, response);
    }
}

____________________________________________________

PROBLEM:

I get the bean in the servlet, that is to say:

bean myBean = (bean)session.getAttribute("bean"); actually returns an object and not null, thats not a problem.

The problem is that the username field is null. I have tried it with radio buttons and numerous other fields and they have all returned null.

If anybody please could help me it would be great. I highly appreciate any time you spend helping me out.

Thanks,
James

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.