Web User Interface for a Java application

后端 未结 7 1423
后悔当初
后悔当初 2020-12-14 03:22

I\'m trying to create a web user interface for a Java application. The user interface is going to be very simple, consisting of a single page with a form for users to pose t

相关标签:
7条回答
  • 2020-12-14 04:18

    EDIT: So far, I've decided on the following:

    • Web Application Server: Jetty;
    • Java Server Pages for the views;
    • Based on the suggestions of @djna, I've read a couple of articles regarding Model 2, and I came up with this solution (which I haven't tested yet, because I need to finish my application before moving into the interface):

    form.jsp

    <form action="/servlet/EngineServlet" method="GET">
      <input type="text" name="text" />
    </form>
    

    EngineServlet.java

    public class EngineServlet extends HttpServlet {
      private Engine engine = new Engine(); 
      // does this make sure engine only gets instantiated one time in the entire lifespan of the web application; from what I've read from the servlet lifecycle, it seems like it, but I'd like to hear opinions
    
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response) {
        String text = request.getParameter("text");
        ResultBean result = engine.run(text);
        request.setAttribute("result", result);
        RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
        dispatcher.forward(request, response); 
        // what's the difference between forward, and request.sendRedirect() ?
      }    
    }
    

    result.jsp

    <div>The result was: ${result.text}</div>
    

    What do you think of this solution? Any problems that may not be obvious to someone coming from a J2SE background ? I also wrote some doubts that I have in the code as comments. Thanks.

    0 讨论(0)
提交回复
热议问题