Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>techniques-- you could probably use an appropriate design pattern to fragment your code in such a way that each jsp page represents "actions" e.g. addFriendAction.jsp. the advantage here is that finding the appropriate page name would be easier as you just have to refer to corresponding action. compare this with having JSP pages where you incorporate multiple actions in the same page. heres an example (Im assuming you're using servlets along with the jsp pages in accordance with the MVC pattern). e.g. using the command pattern to structure your web app into actions (refer Code Example 4.8- <a href="http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html" rel="nofollow">http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html</a>)</p> <p>adding to above, let me share a recent project i did which made use of this pattern. below is my servlet class</p> <pre><code>/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package servlets; import beans.SeekerCustomer; import java.io.*; import java.util.HashMap; import javax.servlet.*; import javax.servlet.http.*; /** * * @author Dhruv */ //servlet class acts as controller by delegating //operations to the respective Action concrete subclass public class ControllerServlet extends HttpServlet { //stores all the possible operation names //and operation objects for quick access private HashMap actions; @Override public void init() throws ServletException { actions = new HashMap(); //all the various operations are stored in the hashmap CreateUserAction cua = new CreateUserAction(new SeekerCustomer()); actions.put(cua.getName(), cua); ValidateUserAction vua = new ValidateUserAction(new SeekerCustomer()); actions.put(vua.getName(), vua); ListNonFriendsAction lnfa = new ListNonFriendsAction(new SeekerCustomer()); actions.put(lnfa.getName(), lnfa); AddFriendAction afa = new AddFriendAction(new SeekerCustomer()); actions.put(afa.getName(), afa); ConfirmFriendReqAction cfra = new ConfirmFriendReqAction(new SeekerCustomer()); actions.put(cfra.getName(),cfra); DeclineFriendReqAction dfra = new DeclineFriendReqAction(new SeekerCustomer()); actions.put(dfra.getName(),dfra); AddImageAction aia = new AddImageAction(new SeekerCustomer()); actions.put(aia.getName(),aia); ViewImageAction via = new ViewImageAction(new SeekerCustomer()); actions.put(via.getName(),via); ViewAllImagesAction vaia = new ViewAllImagesAction(new SeekerCustomer()); actions.put(vaia.getName(),vaia); AddTagAction ata = new AddTagAction(new SeekerCustomer()); actions.put(ata.getName(),ata); ViewTagAction vta = new ViewTagAction(new SeekerCustomer()); actions.put(vta.getName(),vta); ViewAllTagsAction vata = new ViewAllTagsAction(new SeekerCustomer()); actions.put(vata.getName(),vata); ViewProfileAction vpa = new ViewProfileAction(new SeekerCustomer()); actions.put(vpa.getName(),vpa); EditAccountAction epa = new EditAccountAction(new SeekerCustomer()); actions.put(epa.getName(),epa); ViewOthersImageAction voia = new ViewOthersImageAction(new SeekerCustomer()); actions.put(voia.getName(), voia); AddOthersTagAction aota = new AddOthersTagAction(new SeekerCustomer()); actions.put(aota.getName(),aota); LogoutAction loa = new LogoutAction(new SeekerCustomer()); actions.put(loa.getName(), loa); ToptagsAction tts = new ToptagsAction(new SeekerCustomer()); actions.put(tts.getName(), tts); UpdateAccountAction uaa = new UpdateAccountAction(new SeekerCustomer()); actions.put(uaa.getName(), uaa); ViewAllFriendsAction vafa = new ViewAllFriendsAction(new SeekerCustomer()); actions.put(vafa.getName(), vafa); ReturnHomeAction rha = new ReturnHomeAction(new SeekerCustomer()); actions.put(rha.getName(),rha); } public void processRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { //identify the operation from the URL String op = getOperation(req.getRequestURL()); //find and execute corresponding Action Action action = (Action)actions.get(op); Object result = null; try { //maintain the session between requests result = action.perform(req, resp); HttpSession session = req.getSession(); session.setAttribute("session1", result); } catch (NullPointerException npx) { //nothing to handle } } //both GET and POST operations are directed to "processRequest" method @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } //uses the URL to identify the operation private String getOperation(StringBuffer requestURL) { String op=""; //identifies the last index of "/" before ".do" and //uses that to put each character after the "/" into "op" for(int i= requestURL.lastIndexOf("/",requestURL.indexOf(".do"))+1; i&lt;requestURL.indexOf(".do"); i++) { op= op+requestURL.charAt(i); } return op; } } </code></pre> <p>you can see that each action is handled in the main servlet by dispatching it to smaller servlets. <strong>so if you click on CreateUserAction, this action is handled by a CreateUserAction.java servlet, which then redirects the output to CreateUserAction.jsp. this way, using an appropriate pattern fragments your code in such a way that finding the respective JSP page is done easily. that is the point i'm trying to make here- use patterns!</strong></p> <p>templates-- you could make use of JSP templates across pages so that you only need to modify the template to effect common changes across the JSP pages (refer- <a href="http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates/" rel="nofollow">http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates/</a>)</p> <p>a rather naive way would be use IDE shortcuts.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload