Converting this scriptlet code to JSTL / EL?

919 Views Asked by At

I don't know how to recode this scriptlet code to JSTL / EL taglib. The statements are quite simple but it uses session data and make new objects which I don't know how to do in JSTL / EL. Is it possible to recode this to JSTL / EL or do I need to put it in a preprocess servlet? If I need the preprocess servlet then I still don't know how to manage since there is no specific request handler for this view, only an access to the web page that populates a session object to our jsp page. How can I rewrite this to JSTL / EL?

<%
    final Logger logger = Logger.getLogger("arendeprocess_grunduppgifter_visa.jsp");
    ArendeProcessPageController apc = new ArendeProcessPageController(request);
    GrunduppgifterPageController pc = new GrunduppgifterPageController(request);
    String arendeTyp = apc.getArendeTyp();
    boolean showSearch = false;
    int vectr = 0; // får bara användas i errormessages.jspf
    IFormData ifData = PandoraManager.getSessionData(session).getFormData();

    AnsokanInfo ansokanInfo = apc.getAnsokanInfo();
    PersonInfo editPerson = new PersonInfo();
    if(ansokanInfo != null && ansokanInfo.hasEditPersonInfo()) {
        editPerson = ansokanInfo.getEditPersonInfo();
    } else {
        editPerson.setFornamn(apc.getNyregPerson().getFornamn());
        editPerson.setEfternamn(apc.getNyregPerson().getEfternamn());
        editPerson.setForetag(apc.getNyregPerson().getForetag());
        //editPerson.setOrgnr(apc.getNyregPerson().getOrgnr());
        editPerson.setLandKod(apc.getNyregPerson().getLandKod());
    }
    if(apc.getLatestAction().equals("Namnsokning") && apc.getLatestActionCommand().equals("search")) {
        showSearch = true;    
    }
    List<PersonInfo> sokandeList = ansokanInfo.getSokandeList();
    List<PersonInfo> uppfinnareList = ansokanInfo.getUppfinnareList();
    List<PersonInfo> ombudList = ansokanInfo.getOmbudList();
    List<Prioritet> prioriteter = ansokanInfo.getPrioriteter();
    List<Deposition> depositioner = ansokanInfo.getDepositioner();
    request.setAttribute("request", request);
    request.setAttribute("ansokanInfo", ansokanInfo); 
    request.setAttribute("editPerson", editPerson);
    request.setAttribute("apc", apc);
    request.setAttribute("sokandeList", sokandeList);
    request.setAttribute("uppfinnareList", uppfinnareList);
    request.setAttribute("ombudList", ombudList);
    request.setAttribute("GrunduppgifterConstants", new GrunduppgifterConstants());
%>
2

There are 2 best solutions below

0
On BEST ANSWER

Yes anything you can write in scriptlets can be written in EL/Custom Tags.

There are two ways :

  • Change all Controller classes as Use Beans and write the above code using EL.
  • Move the above code to Custom Tags and eliminate scriptlets
0
On

JSTL is intented to control the flow of HTML generation. This scriptlet however doesn't produce any HTML, so replacing by JSTL is completely out of question.

You indeed need a preprocessing servlet instead. I only don't understand what exactly you mean with

I still don't know how to manage since there is no specific request handler for this view, only an access to the web page that populates a session object to our jsp page

But it should be a matter of moving the JSP file in question into /WEB-INF folder so that it can never be accessed directly and creating a servlet which is mapped on the desired URL pattern and does the job in doGet() method and finally forwards to the desired JSP. Assuming that the JSP file is /page.jsp and has been relocated into /WEB-INF/page.jsp, then the servlet would look like this:

@WebServlet("/page.jsp")
public class PageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Put the original scriptlet code here.

        // Keep the following line untouched.
        request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
    }

}