Wednesday, December 18, 2013

Usebean jsp

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <form action ="test.do">
    <body>
       Enter Name:
       <input type="text" name ="name"></input>
       <br>
       Enter Dog Name:
       <input type="text" name ="dname"></input>
       <input type="submit" value="Submit" />
    </body>
    </form>
</html>

result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
     <jsp:useBean id="person" scope="request" class="bean.Person">
            <jsp:setProperty name="person" property="name" param="name"></jsp:setProperty>
            <jsp:setProperty name="person" property="dname" param="dname"></jsp:setProperty>
        </jsp:useBean> 
        Name created by servlet: <jsp:getProperty name="person" property="name"></jsp:getProperty>
        <br>
        Dog Name is:
        ${person.dname}

</body>
</html>

test.do

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String dname = request.getParameter("dname");
       
        Person p=new Person();
        p.setName(name);
        p.setDname(dname);
        request.setAttribute("person", p);
        RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
        rd.forward(request, response);
    }

Person.jsp

package bean;

public class Person {
String name,dname;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDname() {
    return dname;
}

public void setDname(String dname) {
    this.dname = dname;
}


}


No comments:

Post a Comment