Servlet deployment problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evilmonkey
    New Member
    • Jan 2007
    • 14

    #1

    Servlet deployment problems

    My assignment was to create a shopping cart servlet, it works locally on tomcat but when I deploy it to the schools server (also tomcat) it fails to refresh the front page and just prints the HTML Code not the page. Any thoughts or a helping hand would be appreciated.

    Code:
    import java.io.*;
    import java.net.*;
    import java.text.NumberFormat;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /**
     *
     * @author Farrar
     */
    public class Assign04 extends HttpServlet {
        /** 
        * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
        * @param request servlet request
        * @param response servlet response
        */
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException
         {
          // uses the makeHTML() to create the page and session   
          response.setContentType("text/html");
          HttpSession session = request.getSession();
          PrintWriter out = response.getWriter();
          out.println(makeHTML(session));
          out.close();
        }
    
        /** 
        * Handles the HTTP <code>POST</code> method.
        * @param request servlet request
        * @param response servlet response
        */
      protected void doPost(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException, IOException
        {
          String msg;
    
          HttpSession session = req.getSession(true);
          PrintWriter out1 = res.getWriter();
          NumberFormat formatter = NumberFormat.getCurrencyInstance();
          if (req.getParameter("addclick") != null && req.getParameter("item") != null &&
                  req.getParameter("item").length() > 0)
          {
              String itemName = req.getParameter("item").toLowerCase().trim();
              String qty = req.getParameter("quantity");
              if (qty == null) 
                  qty = "1";
              if (session.getAttribute(itemName) == null)
              {
                  session.setAttribute(itemName, Integer.parseInt(qty));
              }
              else
              {
                  int oldQuantity =  (Integer)session.getAttribute(itemName);
                  session.setAttribute(itemName, oldQuantity + Integer.parseInt(qty));
              }
              // sets oldItems to default to 0  
              int oldNumItems = 0;
              // checks to see if numItem sess attrib is not null if it is oldItem is
              // set to =(Integer)session.getAttribute("numItems")
              if (session.getAttribute("numItems") != null)
                  oldNumItems = (Integer)session.getAttribute("numItems");
              // checks to see if item parameter attrib is not null and is not empty takes the 
              //it incements the total Number of Items by the quantity 1 
              if (req.getParameter("item") != null && !req.getParameter("item").isEmpty())
                session.setAttribute("numItems", Integer.parseInt(qty) + oldNumItems);
              // sets oldTotalCost to 0.00 default 
              double oldTotalCost = 0.00;
              // if totalCost sess attrib is not null oldTotalCost = session.getAttribute 
              if (session.getAttribute("totalCost") != null)
                  oldTotalCost = (Double)session.getAttribute("totalCost");
              
              // logic for currentCost takes the itemName and multiplies it by 10
              double currentCost = itemName.length() * 10.0* Integer.parseInt(qty);
              // sets the session attribute totalCost  to oldTotalCost plus 
              session.setAttribute("totalCost", oldTotalCost + currentCost);
              out1.println(makeHTML(session));
          }
          // kills the session by invalidating it 
          else if (req.getParameter("checkoutclick") != null)
          {
              session.invalidate();
              out1.println(makeCheckoutHTML(session));
          }
          //redirects the user to the view04 page to see the shopping list table  
          else if (req.getParameter("viewclick") != null )
          {
             res.sendRedirect("/FarrarJD04/servlet/assign04.View04"); 
          }
          else
          {
            out1.println(makeHTML(session));
          }
       }
      //makes the checkout HTML page 
      public String makeCheckoutHTML(HttpSession session)
      {
          String docType =
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
            "Transitional//EN\">\n";
          
          String checkoutPage = docType + 
                  "<html><head>" +
                  "</head>" + 
                  "<body bgcolor='#C35817'>\n" +
                  "<p align=center><h4>Thank you for shopping!  Goodbye!</h4>\n" +
                  // link for debugging 
                   "<a href='/FarrarJD04/servlet/assign04.Assign04'>Go Shopping!</a></div>\n" +
                  "</body>\n" +
                  "</html>";
          return checkoutPage;
      }
      // makes the HTMLpage used to for Assign04  
      public String makeHTML(HttpSession session)
       {
           
           String docType =
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
            "Transitional//EN\">\n";
          int numItems = 0;
           //gets the session Attribute numItems if it is not null and casts that object to a 
          // interger called numItems.  
          if (session.getAttribute("numItems") != null)
            numItems = (Integer)session.getAttribute("numItems");
              
          double cost = 0.00;
          //gets the session Attribute totalcost if it is not null and casts that object to a 
          // double called cost. 
          if (session.getAttribute("totalCost") != null)
              cost = (Double)session.getAttribute("totalCost");
          
          String itemPage = docType +
            "<html><head>" +
            "<title>Web Assignment 04</title>" +
            "</head>" +
            "<BODY BGCOLOR='#C35817' onLoad = document.itemInput.item.focus();>\n" +
            "<center>\n" +
            "<h1><Big><i>Joe Farrar's General Store</i></Big></h1>\n" +
            "<h2>dont bother turning off the cookies </h2>\n" +
            "<FORM name=itemInput Action='/FarrarJD04/servlet/assign04.Assign04' Method=Post>\n" + 
            "Item to buy <INPUT TYPE='TEXT' NAME='item'></INPUT>\n" + 
            "<INPUT TYPE='Hidden' name='quantity' value='1'></INPUT><br><br>\n" +
            "<INPUT TYPE='SUBMIT' name='addclick' VALUE='Add to Cart'></INPUT>\n" +
            "<INPUT TYPE='SUBMIT' name='viewclick' VALUE='View Cart'></INPUT>\n" +
            "<INPUT TYPE='SUBMIT' name='checkoutclick' VALUE='Checkout'></INPUT><br><br>\n" +
            "</form>\n" +
            "<table align='center'>\n" +
            "<tr>" +
            "<th>Item Count</th>\n" +
            "<th>Total Cost</th>\n" +
            "</tr>" +
            "<tr>" +
            "<td>" + numItems + "</td>\n" +
            "<td>" + cost + "0</td>\n" +
            "</tr>\n" +
            "</table>\n" +
            "</center>\n" +
            "</body>\n" +
            "</html>";
         
          return itemPage;
       }
        
      }
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    I think the problem is with ....

    [code=java]
    response.setCon tentType("text/html"); //i think it's worng
    //it should be ......
    response.setCon tentType("text/html;characters et=utf-8");
    [/code]

    have a try it ... i think it will work...

    debasis jana

    Comment

    • ajos
      Contributor
      • Aug 2007
      • 283

      #3
      Originally posted by dmjpro
      I think the problem is with ....

      [code=java]
      response.setCon tentType("text/html"); //i think it's worng
      //it should be ......
      response.setCon tentType("text/html;characters et=utf-8");
      [/code]

      have a try it ... i think it will work...

      debasis jana
      I think it may be the versioning problem.which version of web container you are using?also check out the version of your schools server,Im not sure though.
      regards,
      ajos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        If it works on your machine and doesn't work on the school's server then you have not deployed it correctly on the school's server. If you are seeing the HTML code then the page is not being run on the server at all.

        Comment

        • evilmonkey
          New Member
          • Jan 2007
          • 14

          #5
          response.sendRe direct( ) was what i needed to do, Instead of trying to recreate the page I just needed to redirect back to the front end, the logic and session took care of the rest. As usual I was looking for the problem in the wrong place
          Thanks for your replies.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by evilmonkey
            response.sendRe direct( ) was what i needed to do, Instead of trying to recreate the page I just needed to redirect back to the front end, the logic and session took care of the rest. As usual I was looking for the problem in the wrong place
            Thanks for your replies.
            Doesn't explain why it worked on your local machine does it?

            Comment

            • evilmonkey
              New Member
              • Jan 2007
              • 14

              #7
              Originally posted by r035198x
              Doesn't explain why it worked on your local machine does it?
              I don't know why it worked in my IDE and honestly wish it didn't. It would have saved me from having to write it twice. Maybe has something to do with Strings being immutable

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by evilmonkey
                I don't know why it worked in my IDE and honestly wish it didn't. It would have saved me from having to write it twice. Maybe has something to do with Strings being immutable
                Believe me it has nothing to do with the immutability of Strings!

                Comment

                Working...