i am a student i am doing a jsp project for my J2EE module.I choose a auction web site 4 my project. I have a doubt Can I use the java beans into a Servlet.I uses the Beans into my Jsp Page by using USEBEAN but how can i use the sanme bean into a Servlet.????.
Please Reaply me.
Thanks
thabo

Dani AI

Generated

Short answer: yes — a JavaBean is just a plain Java object, so servlets can create one, call its methods (including validation), and put it into request/session scope for a JSP to read. This builds on 's basic bean explanation and on 's point: learn the Java basics first so you understand objects, packages and classpaths.

A simple, practical pattern for form processing:

  • Servlet (controller) receives POST, sets request encoding, reads parameters.
  • Populate a bean (POJO) with setters or a constructor, then call a validate() method on the bean (or call a separate validator).
  • If validation fails, attach the bean and an errors map to the request and forward back to the form JSP.
  • If validation passes, persist the data and redirect to a success page (use redirect to avoid form resubmission).

Example bean and servlet sketches (not the same code already posted):

public class Bid implements java.io.Serializable {
    private String bidder;
    private double amount;
    public Bid() {}
    // getters/setters omitted for brevity

    public Map<String,String> validate() {
        Map<String,String> errors = new HashMap<>();
        if (bidder == null || bidder.trim().isEmpty()) errors.put("bidder","Required");
        if (amount <= 0) errors.put("amount","Must be > 0");
        return errors;
    }
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    Bid b = new Bid();
    b.setBidder(req.getParameter("bidder"));
    try { b.setAmount(Double.parseDouble(req.getParameter("amount"))); } catch(...) {}
    Map<String,String> errors = b.validate();
    if (!errors.isEmpty()) {
        req.setAttribute("errors", errors);
        req.setAttribute("bid", b);
        req.getRequestDispatcher("/bidForm.jsp").forward(req, resp);
        return;
    }
    // save and redirect
    resp.sendRedirect(req.getContextPath()+"/bidSuccess.jsp");
}

Tips and common pitfalls: make sure bean classes are in WEB-INF/classes (or a jar in WEB-INF/lib) and packages are correct; prefer servlets for control logic and JSP+JSTL/EL for view (avoid scriptlets); always set request encoding for POST forms; and avoid storing transient request state only in session. For more depth, study the servlet/JSP MVC pattern and practice small examples before wiring a full auction site. ’s advice to read a good book/tutorial first is sound.

Recommended Answers

All 5 Replies

Read and understand the java tutorial first of all.
You seem to have no grasp of what a javabean is, which means you don't understand the basics of the language.

Read and understand the java tutorial first of all.
You seem to have no grasp of what a javabean is, which means you don't understand the basics of the language.

i didnt yet understand what is bean...i also show some tutorials....can you give a small explanation about bean and how it is differ from servlet?and give and example for a form processing

JavaBeans are Java classes which adhere to an extremely simple coding convention. All you have to do is to implement java.io.Serializable interface, use a public empty argument constructor and provide public getter and setter methods to get and set the values of private variables ( properties )

Sample Example Code:

public class SimpleBean implements java.io.Serializable {

	/* Properties */
	private String name = null;
	private int age = 0;

	/* Empty Constructor */
	public SimpleBean() {}

	/* Getter and Setter Methods */
	public String getName() {
		return name;
	}

	public void setName(String s) {
		name = s;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int i) {
		age = i;
	}
}

Thank u Sapna Suresh.. But i have a doubt can we invoke some other user defined method into the beans?.Because you said the bean is a java class.... i want to make validation to my inputs..it means ,for an example i passes all the input value to a bean from the form i have made.i want to validate my inputs..can you explain this senario??please give a example cording for your explain...

Thank u Sapna Suresh.. But i have a doubt can we invoke some other user defined method into the beans?.Because you said the bean is a java class.... i want to make validation to my inputs..it means ,for an example i passes all the input value to a bean from the form i have made.i want to validate my inputs..can you explain this senario??please give a example cording for your explain...

Then you should seriously pick up some book and start learning as jwenting already recommended as you obviously do not have essential background

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.