3

I has a jsp page (index.jsp) with a form with two text fileds username and password like.

 <form action="MyClass">
        <input type="text" name="username" id="username" />
        <input type="password" name="password" id="password" />
        <input type="submit" />
  </form>

On form submition i am invocking a servlet. I know that we can get the entered username and password values by using request methods,

  request.getParameter("username");
  request.getParameter("password");

But i don't want to use them , instead i want to store these values in a bean called BeanClass and i want to retrieve values from the bean in the sevlet. How can i get it??

user636207
  • 286
  • 2
  • 6
  • 14

2 Answers2

2

You have to use <jsp:useBean/> action to instantiate the BeanClass with request or session scope in JSP.

Sample - EmpServlet.java

package com.me;

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

public class EmpServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         PrintWriter pw=response.getWriter();
         Emp emp=(Emp)request.getAttribute("emp");
         pw.print(emp);
    }
}

Emp.java : Emp bean

package com.me;
public class Emp {
    private int age;
    private String name;
    public Emp() {
        name="";
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public boolean valid()
    {
        return age!=0 && name.length()!=0;
    }
    @Override
    public String toString() {
        return "Emp{" + "age=" + age + ", name=" + name + '}';
    }
}

emp.jsp (view)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="emp" class="com.me.Emp" scope="request">
    <jsp:setProperty name="emp" property="*"/>
</jsp:useBean>

<c:if test="${emp.valid()}">
    <jsp:forward page="emp"/>
</c:if>
<form method="post" action="emp.jsp">
    <br/><input type="text" name="age"/>
    <br/><input type="text" name="name"/>
    <br/><input type="submit"/>
</form>

web.xml

<servlet>
    <servlet-name>EmpServlet</servlet-name>
    <servlet-class>com.me.EmpServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>EmpServlet</servlet-name>
    <url-pattern>/emp</url-pattern>
</servlet-mapping>
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • can i use this statement in index.jsp it self.. If so where to place this statement in jsp page.. and i also tried to get the valeus using getter methods from servlet but iam getting null values – user636207 Jan 19 '12 at 01:59
1

To the point, you're looking for a MVC framework like JSF or Spring MVC. With JSF it'll look something like this:

<h:form>
    <h:inputText value="#{bean.username}" required="true" />
    <h:inputSecret value="#{bean.password}" required="true" />
    <h:commandButton value="submit" action="#{bean.submit}" />
    <h:messages />
</h:form>

with

@ManagedBean
@RequestScoped
public class Bean {

    private String username;
    private String password;

    public void submit() {
        // Do here your job.
    }

    // Add/generate getters and setters.
}

That's all. No need for a servlet.

If you really want to do it the low level servlet way, you'd need to populate the bean yourself. This can be convenienced with Apache Commons BeanUtils to save boilerplate code.

Bean bean = new Bean();
BeanUtils.populate(bean, request.getParameterMap());
request.setAttribute("bean", bean);
// ...

The <jsp:useBean> does not allow for the MVC approach, it's more a MV. You have to mingle the conversion/validation into model and control the request/response inside the view, tasks which a controller should do. MVC frameworks offer you a controller which takes all this nasty boilerplate tasks from your hands.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555