design - Passing arguments between two JSP pages -
i'm new web programming, , stuck on designing simple flow.
let's have simple object like,
public class object1 { private int id; private string text; { getters , setters etc} }
i bunch(depends on record number in db) of these dao list<object1>
, , print them in loop on page, lets call jsppage1
. , have page, jsppage2
, object1.id
what want is:
create kind of connection, such that, when user clicks or selects object, selected object's id (on jsppage1
) sent jsppage2
.
what i've thought far;
create hyperlinks redirecting id information, example:
<c:foreach items="${objects}" var="object1"> <tr> <td><a href="/jsppage2?id=${object1.id}">${object1.text} </a></td> </tr> </c:foreach>
this stinks bad, can tamper address bar , change id value.
i obfuscate argument information on option #1. seems little more safer, willful user can still mingle created hash.
- i write scriptlets like,
request.setattribute("id", object1.id);
i've been told including code , logic in jsp pages wrong thing do.
so repeat question; there way of doing things in neater way ?
or
is way of approaching problem fundamentally wrong , should consider different design. (if tips on that)
one way can think of use mvc approach. once user clicks on url, can have servlet verify object id valid. in other words, servlet contain logic. so, in jsppage1
<c:foreach items="${objects}" var="object1"> <tr> <td><a href=" <c:url value='/checkid' > <c:param name='id'value ='${object1.id}' /> </c:url> ">${object1.text}</a> </tr> </c:foreach>
then register servlet called checkidservlet in web.xml
<servlet> <servlet-name>checkid</servlet-name> <servlet-class>checkidservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>checkid</servlet-name> <url-pattern>/checkid</url-pattern> </servlet-mapping>
since passing parameters in url need override doget(...) method
public class checkidservlet extends httpservlet { @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //do id checking make sure it's number or if id value in database //if either condition not met, redirect jsppage 1 or want //handle response.sendredirect("/jsppage1.jsp"); //or if id valid requestdispatcher dispatcher = getservletcontext().getrequestdispatcher("/jsppage2.jsp"); dispatcher.forward(request, response); //keep in mind pseudocode make sure add if conditions above }
finally, in jsppage2 display object id
<h1>${param.id}</h1>
Comments
Post a Comment