Reputation: 1628
How to populate a form when JSP is called first time ? I need to process an XML file when a page is refreshed & populate the form in the JSP page?(not using frameworks)
Upvotes: 0
Views: 3945
Reputation: 1108537
You have 2 options:
Use <jsp:usebean>
, let the bean fill itself in the constructor.
<jsp:useBean id="bean" class="com.example.Bean" />
...
<input name="foo" value="${fn:escapeXml(bean.foo)}">
<input name="bar" value="${fn:escapeXml(bean.bar)}">
The fn:escapeXml()
is not mandatory for the functioning, but it's mandatory to prevent XSS attacks if you're redisplaying user-controlled input in a HTML page.
Use the doGet()
method of a Servlet.
Bean bean = new Bean();
request.setAttribute("bean", bean);
request.getRequestDispatcher("page.jsp").forward(request, response);
and use the URL to servlet in browser address bar instead of the JSP's one. You can use the same code in JSP page as above, expect of the jsp:useBean
line.
Way 1 is more the oldschool and student's way. Way 2 is more MVC oriented and preferred in this particular case since you seem to be doing more than just populating a bean.
Upvotes: 4
Reputation: 43560
Sure - just set initial values in the bean properties that you are exposing in your form. Then use expression language to populate the fields, such as
<input type="text" value="${bean.myProperty}"/>
Upvotes: 0