Never 'Run' a Web App again!

No..

The last web app hasn't been developed... though some of the ideas that I have seen floated on TechCrunch seem like they are 'A Web App Too Far'. I am talking about how I "do" web-app development in NetBeans.

When you are debugging a Java application in NetBeans, you have the option to 'Apply Code Changes'. You can read this chapter of the NetBeans Field Guide about debugging that describes the 'Apply Code Changes' feature.

You can use this feature of the debugger to significantly reduce the number of times that you deploy a web app that has been targeted to GlassFish.

The best explanation is an example... [To play along at home, I recommend that you get NetBeans 6.0 and GlassFish V2.]

  1. Start the IDE
  2. Create a new Web Application project that targets a GlassFish domain. (I will call my project WA1 [double-u a one])
  3. Create a new Servlet in the project.
  4. Use the Debug item on the project's right-click menu.
    (This compiles the project code, starts the server in debug mode, attaches the NetBeans debug session to the server's JVM, deploys the web app and opens the browser on something like http://localhost:8080/WA1. When the browser opens, it will probably say something like "Hello World!" and the browsers title bar will probably say something like "JSP Page".)
  5. Change the code in the servlet that you created two steps back from
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
                /\* TODO output your page here
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet NewServlet</title>");  
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
                out.println("</body>");
                out.println("</html>");
                \*/
            } finally { 
                out.close();
            }
        } 
    
    TO
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
                //\* TODO output your page here
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet NewServlet</title>");  
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
                out.println("</body>");
                out.println("</html>");
                //\*/
            } finally { 
                out.close();
            }
        } 
    
    Notice that the changes are in the commenting style of the TODO line and the closing asterisk-slash...
  6. Press the 'Apply Code Changes' icon in the Debug toolbar or use the Apply Code Changes item from the Run menu.
  7. Switch to your browser and change the URL in the address bar from (something like) http://localhost:8080/WA1 to (something like) http://localhost:8080/WA1/NewServlet.
    At this point the browser should have something like
    Servlet NewServlet at /WA1
    
    in the main window and 'Servlet NewServlet' in the title bar.
So there you have it. You just made a change to a running web application and the changes were applied to the running web app WITHOUT a deploy.

FINE PRINT: This depends on a couple of very important things.

  1. Directory deployment of web applications to Glassfish. If you turn off directory deployment, this will not work. The updated class needs to get to the right directory for the server to get the changed class file. Archive deployment put the class into directories under the domain's "root".
  2. The web app needs to be "stand-alone". A web app that is deployed as part of an enterprise application project will not compile the code for the changed class into the directory where the server expects it to be. The build process of the enterprise application copies the class file of sub module to a special location, to meet the server's exploded application layout constraints.
  3. Doesn't apply to classes that are part of a Java Library project that used by your web app. This is really similar to the difference between a stand alone web app and a web app inside an enterprise application project.
  4. The change that you make to the class is fairly constrained. If your change "breaks the rules", the 'Apply Code Changes' processing will fail. The error message will look something like this
    The virtual machine does not support this operation: schema change not implemented
    /Users/vbk/NetBeansProjects/WA1/nbproject/build-impl.xml:616: ...
    /Users/vbk/NetBeansProjects/WA1/nbproject/build-impl.xml:232: ...
    BUILD FAILED (total time: 0 seconds)
    
    The class that you were trying to replace will be unharmed on the server. If you run into this type of error, you can just use the project's Debug item to recompile the project and redeploy it.

Comments:

Post a Comment:
Comments are closed for this entry.
About


Vince Kraemer writes the entries in this blog.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
News
Blogroll

No bookmarks in folder

automarks