473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Servlet deployment problems

evilmonkey
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.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3. import java.text.NumberFormat;
  4. import java.util.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8. /**
  9.  *
  10.  * @author Farrar
  11.  */
  12. public class Assign04 extends HttpServlet {
  13.     /** 
  14.     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  15.     * @param request servlet request
  16.     * @param response servlet response
  17.     */
  18.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  19.               throws ServletException, IOException
  20.      {
  21.       // uses the makeHTML() to create the page and session   
  22.       response.setContentType("text/html");
  23.       HttpSession session = request.getSession();
  24.       PrintWriter out = response.getWriter();
  25.       out.println(makeHTML(session));
  26.       out.close();
  27.     }
  28.  
  29.     /** 
  30.     * Handles the HTTP <code>POST</code> method.
  31.     * @param request servlet request
  32.     * @param response servlet response
  33.     */
  34.   protected void doPost(HttpServletRequest req, HttpServletResponse res)
  35.               throws ServletException, IOException
  36.     {
  37.       String msg;
  38.  
  39.       HttpSession session = req.getSession(true);
  40.       PrintWriter out1 = res.getWriter();
  41.       NumberFormat formatter = NumberFormat.getCurrencyInstance();
  42.       if (req.getParameter("addclick") != null && req.getParameter("item") != null &&
  43.               req.getParameter("item").length() > 0)
  44.       {
  45.           String itemName = req.getParameter("item").toLowerCase().trim();
  46.           String qty = req.getParameter("quantity");
  47.           if (qty == null) 
  48.               qty = "1";
  49.           if (session.getAttribute(itemName) == null)
  50.           {
  51.               session.setAttribute(itemName, Integer.parseInt(qty));
  52.           }
  53.           else
  54.           {
  55.               int oldQuantity =  (Integer)session.getAttribute(itemName);
  56.               session.setAttribute(itemName, oldQuantity + Integer.parseInt(qty));
  57.           }
  58.           // sets oldItems to default to 0  
  59.           int oldNumItems = 0;
  60.           // checks to see if numItem sess attrib is not null if it is oldItem is
  61.           // set to =(Integer)session.getAttribute("numItems")
  62.           if (session.getAttribute("numItems") != null)
  63.               oldNumItems = (Integer)session.getAttribute("numItems");
  64.           // checks to see if item parameter attrib is not null and is not empty takes the 
  65.           //it incements the total Number of Items by the quantity 1 
  66.           if (req.getParameter("item") != null && !req.getParameter("item").isEmpty())
  67.             session.setAttribute("numItems", Integer.parseInt(qty) + oldNumItems);
  68.           // sets oldTotalCost to 0.00 default 
  69.           double oldTotalCost = 0.00;
  70.           // if totalCost sess attrib is not null oldTotalCost = session.getAttribute 
  71.           if (session.getAttribute("totalCost") != null)
  72.               oldTotalCost = (Double)session.getAttribute("totalCost");
  73.  
  74.           // logic for currentCost takes the itemName and multiplies it by 10
  75.           double currentCost = itemName.length() * 10.0* Integer.parseInt(qty);
  76.           // sets the session attribute totalCost  to oldTotalCost plus 
  77.           session.setAttribute("totalCost", oldTotalCost + currentCost);
  78.           out1.println(makeHTML(session));
  79.       }
  80.       // kills the session by invalidating it 
  81.       else if (req.getParameter("checkoutclick") != null)
  82.       {
  83.           session.invalidate();
  84.           out1.println(makeCheckoutHTML(session));
  85.       }
  86.       //redirects the user to the view04 page to see the shopping list table  
  87.       else if (req.getParameter("viewclick") != null )
  88.       {
  89.          res.sendRedirect("/FarrarJD04/servlet/assign04.View04"); 
  90.       }
  91.       else
  92.       {
  93.         out1.println(makeHTML(session));
  94.       }
  95.    }
  96.   //makes the checkout HTML page 
  97.   public String makeCheckoutHTML(HttpSession session)
  98.   {
  99.       String docType =
  100.       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  101.         "Transitional//EN\">\n";
  102.  
  103.       String checkoutPage = docType + 
  104.               "<html><head>" +
  105.               "</head>" + 
  106.               "<body bgcolor='#C35817'>\n" +
  107.               "<p align=center><h4>Thank you for shopping!  Goodbye!</h4>\n" +
  108.               // link for debugging 
  109.                "<a href='/FarrarJD04/servlet/assign04.Assign04'>Go Shopping!</a></div>\n" +
  110.               "</body>\n" +
  111.               "</html>";
  112.       return checkoutPage;
  113.   }
  114.   // makes the HTMLpage used to for Assign04  
  115.   public String makeHTML(HttpSession session)
  116.    {
  117.  
  118.        String docType =
  119.       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  120.         "Transitional//EN\">\n";
  121.       int numItems = 0;
  122.        //gets the session Attribute numItems if it is not null and casts that object to a 
  123.       // interger called numItems.  
  124.       if (session.getAttribute("numItems") != null)
  125.         numItems = (Integer)session.getAttribute("numItems");
  126.  
  127.       double cost = 0.00;
  128.       //gets the session Attribute totalcost if it is not null and casts that object to a 
  129.       // double called cost. 
  130.       if (session.getAttribute("totalCost") != null)
  131.           cost = (Double)session.getAttribute("totalCost");
  132.  
  133.       String itemPage = docType +
  134.         "<html><head>" +
  135.         "<title>Web Assignment 04</title>" +
  136.         "</head>" +
  137.         "<BODY BGCOLOR='#C35817' onLoad = document.itemInput.item.focus();>\n" +
  138.         "<center>\n" +
  139.         "<h1><Big><i>Joe Farrar's General Store</i></Big></h1>\n" +
  140.         "<h2>dont bother turning off the cookies </h2>\n" +
  141.         "<FORM name=itemInput Action='/FarrarJD04/servlet/assign04.Assign04' Method=Post>\n" + 
  142.         "Item to buy <INPUT TYPE='TEXT' NAME='item'></INPUT>\n" + 
  143.         "<INPUT TYPE='Hidden' name='quantity' value='1'></INPUT><br><br>\n" +
  144.         "<INPUT TYPE='SUBMIT' name='addclick' VALUE='Add to Cart'></INPUT>\n" +
  145.         "<INPUT TYPE='SUBMIT' name='viewclick' VALUE='View Cart'></INPUT>\n" +
  146.         "<INPUT TYPE='SUBMIT' name='checkoutclick' VALUE='Checkout'></INPUT><br><br>\n" +
  147.         "</form>\n" +
  148.         "<table align='center'>\n" +
  149.         "<tr>" +
  150.         "<th>Item Count</th>\n" +
  151.         "<th>Total Cost</th>\n" +
  152.         "</tr>" +
  153.         "<tr>" +
  154.         "<td>" + numItems + "</td>\n" +
  155.         "<td>" + cost + "0</td>\n" +
  156.         "</tr>\n" +
  157.         "</table>\n" +
  158.         "</center>\n" +
  159.         "</body>\n" +
  160.         "</html>";
  161.  
  162.       return itemPage;
  163.    }
  164.  
  165.   }
  166.  
Oct 31 '07 #1
7 1753
dmjpro
2,476 2GB
I think the problem is with ....

Expand|Select|Wrap|Line Numbers
  1. response.setContentType("text/html"); //i think it's worng
  2. //it should be ......
  3. response.setContentType("text/html;characterset=utf-8");
  4.  
have a try it ... i think it will work...

debasis jana
Nov 1 '07 #2
ajos
283 100+
I think the problem is with ....

Expand|Select|Wrap|Line Numbers
  1. response.setContentType("text/html"); //i think it's worng
  2. //it should be ......
  3. response.setContentType("text/html;characterset=utf-8");
  4.  
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
Nov 1 '07 #3
r035198x
13,262 8TB
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.
Nov 1 '07 #4
response.sendRedirect( ) 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.
Nov 1 '07 #5
r035198x
13,262 8TB
response.sendRedirect( ) 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?
Nov 1 '07 #6
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
Nov 1 '07 #7
r035198x
13,262 8TB
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!
Nov 1 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nischal Topno | last post by:
Hi, Is there any way to auto load/instantiate a new class of a servlet into the JVM (with shutting the web server) after replacing the old class with new one? For e.g., after a servlet is...
1
by: Ric | last post by:
thx for the help. im having problems with java and jsp. i think im not settup up the enviornmental variables right in w2k advanced tab. im using a wrox book, but the wrox book references a servlet...
1
by: Hai Tran | last post by:
Any help is appreciated. Installed Tomcat 4.1 and Mysql on a WinXP. I've manage to get Tomcat up and was able to view my first application ( myhome ) simple home page. Tomcat was installed under...
3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
0
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
4
by: itgaurav198 | last post by:
Hi, I have one jsp and one servlet. I am using a jsp in which i am entering some values like name, empid etc. and there is a dropdown list for location. I want when i select a particular...
2
by: krishna81m | last post by:
In a general servlet class after deployment, you can access the application root as servletContext.getRealPath("/") to access any of the properties files in the "WEB-INF" directory or anywhere else...
3
by: krishna81m | last post by:
Hello, I am looking for a solution to update a .jsp page (kind of progress bar) which shows current progress in a huge simulation when users have to wait for longer periods and the way to do it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.