0

I've created a Spring Roo project. Everything looks fine. Now I want to add a form with a text input and a button to my index.jspx. This form will change a static field currentUser in my ToDo class. So I'm adding:

<form>
  <%@ page import="static com.mypack.domain.ToDo.*" %>
   <label for="_username_id">My name is:</label>
    <% currentUser = request.getParameter("username"); %>
             <input type="text"  id="username" name="username" maxlength="30" path="username" size="0" value="<%= currentUser %>"/>
             <input type="submit"/>
</form>

somewhere in the middle of it. And now it won't work:

This page contains the following errors:

error on line 6 at column 20: StartTag: invalid element name
Below is a rendering of the page up to the first error.

function readCookie(name) { var nameEQ = name + '='; var ca = document.cookie.split(';'); for(var i=0;i

If I comment the lines above, it works just fine. What is wrong? Is there a way to write a value to a static field of a class from a jsp page? How do I work around this?

George
  • 8,368
  • 12
  • 65
  • 106

1 Answers1

1

Just use EL. The request parameters are available by ${param.name}.

<input type="text" name="username" value="${param.username}" />

You only need to take XSS into account as well. Use JSTL fn:escapeXml for this:

<input type="text" name="username" value="${fn:escapeXml(param.username)}" />

Don't use scriptlets (those <% %> things). They are considered bad practice.

That the ToDo.currentUser is a static field also doesn't sound good. Its value would be shared among all requests/sessions in the webapplication. Different visitors will see the same value and any modifications will be reflected to all visitors. Is that what you want?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So using EL, how do I write my value to ToDo.currentUser field? – George Apr 27 '10 at 01:02
  • Or how do I access that `${param.username}` from my controller? – George Apr 27 '10 at 01:07
  • You normally submit the form through a servlet and process it there. In a servlet you can control/preprocess/postprocess requests and freely write Java code. Here are some good tutorials to learn about the basic concepts of JSP/Servlet/MVC: http://courses.coreservlets.com/Course-Materials/csajsp2.html As next step, you can use a MVC framework which is built on top of the Servlet API to do this all more transparently. Spring Roo is one of them. Did you read their tutorials/documentation at http://www.springsource.org/roo as well? – BalusC Apr 27 '10 at 01:10
  • To access a request parameter in a servlet, just do the same as you did in the JSP. In a MVC framework, there shouldn't be any need to grab the parameter yourself, the MVC framework should already have set it for you at the point you want to invoke the action. – BalusC Apr 27 '10 at 01:19
  • I just don't see any `RequestParameters` class, or instance, or anything like that in my controller. In my JSP I had a `request` object. But in controller I don't. – George Apr 27 '10 at 01:29
  • In a servlet, it's the passed-in `HttpServletRequest` object. In a MVC framework, it should usually already have mapped and set it in the model object (reread their docs/tuts how to do it). Else it's available by the context through a straightforward method like `context.getRequestParameterMap()`. – BalusC Apr 27 '10 at 01:46