0

I have a server that is hosting a JSP page. Can I populate it's text boxes from my client's database?

  • If you have access to your client's DB and it is connectable from your server then **Yes** – jmj Apr 29 '11 at 09:04

2 Answers2

1

Create a servlet which loads the data, puts it in request scope and forwards the request to the JSP. If you want to do it whenever the client opens a link/bookmark, then do it in the doGet() method. Or when you want to do it when the client submits a form, then do it in the doPost() method.

Here's an example which preloads a specific product from the DB based on request parameter:

Product product = productService.find(request.getParameter("id")); // Do your DB access job.
request.setAttribute("product", product); // It'll be available by ${product}.
request.getRequestDispatcher("/WEB-INF/product.jsp").forward(request, response); // Let JSP display it.

Map this servlet on an URL pattern of /product then you'll be able to call it by http://example.com/somecontext/product?id=123

In the JSP you just have to set the value attribute of the HTML input element to display it as value of the input element. Since this is sensitive to XSS attacks when you print it plain like as suggested in the other answer, you'd like to use JSTL fn:escapeXml() to avoid XSS attacks.

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<input type="text" name="name" value="${fn:escapeXml(product.name)}" />
<input type="text" name="description" value="${fn:escapeXml(product.description)}" />
<input type="text" name="price" value="${fn:escapeXml(product.price)}" />

Note that scriptlets (those <% %> things) are a poor practice and don't offer instant access to request attributes (i.e. <%= product.getName() %> style as suggested in other answer won't work), nor does it offer standard XSS escaping facility.

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

Can I populate it's text boxes from my client's database?

Yes you can.

Steps:-

  1. Connect to database in servlet.
  2. Retrieve data in servlet and pass it to jsp.
  3. Get that data from request in jsp.
  4. Display data in jsp using scriptlet or jstl.

to populate data in text box in jsp use following:

suppose you have User object that holds user's information then ...

<input type="text" value="<%= user.getName()%>" />
Harry Joy
  • 58,650
  • 30
  • 162
  • 207