How to avoid duplication

  Abstract: How to avoid duplication 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> More Java Pitfalls This is the Chinese version of an example. 
  First, what is to repeat 
  Imagine a visit of a very big booking sites when booking tickets click on the submit button, the server may be slow processing speed, tickets still can not see the result that the failure of the request to buy their own, so once again and over click Submit button, and if not addressed, these requests will be dealt with the server, which lead to false results. 

  Second, to prevent repeat 

  Web.xml looked at (the examples of directories and files download) 

  <? Xml version = "1.0"> 
  Web-app PUBLIC "- / / Sun Microsystems, Inc / / DTD Web Application 2.3 / / EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> 

  <table Width="732" border="0"> <tr> <td width="442"> 


ControllerServlet
ControllerServlet
ControllerServlet
  Org.javapitfalls.item36.ControllerServlet 

  Id 

  Id 





  Form 
  / TicketForm.jsp 

  </ Td> <td width="280"> </ td> </ tr> </ table> 

  Success 
  / Success.jsp 



  Resubmit 
  / ResubmitError.jsp 




ControllerServlet
  / ControllerServlet 






  Look at the form ticketForm.jsp 
  <% @ Page contentType = "text / html; GBK charset ="%> 






<%
  / / The first time, the target set in a session id attribute to the success will be deleted.    After the submission of this test will be whether the attribute value is null judge to repeat. 
  If (session.isNew ()) ( 
  Session.setAttribute ( "id", session.getId ()); 
  ) 
%>

  Ozzie concerts online booking 



  <form Method="get" action="/resubmit/ControllerServlet"> 

  <table Border="1" width="50%"> 
<tr>
  <td> The number of votes: </ td> 
<td>
  <select Name="numTickets"> 
  <option Value="Please enter a Ticket #"> Please enter a Ticket # </ option> 
  <Option value = "1 Ticket" <% if (resubmit.getNumTickets () == "a Ticket") out.println ( "SELECTED");%>> 1 Ticket </ option> 
  <Option value = "2 Tickets" <% if (resubmit.getNumTickets () == "Tickets") out.println ( "SELECTED");%>> 2 Tickets </ option> 
  <Option value = "Tickets" <% if (resubmit.getNumTickets () == "Tickets") out.println ( "SELECTED");%>> 3 Tickets </ option> 
  <Option value = "Tickets" <% if (resubmit.getNumTickets () == "Tickets") out.println ( "SELECTED");%>> 4 Tickets </ option> 
  <Option value = "5 Tickets" <% if (resubmit.getNumTickets () == "5 Tickets") out.println ( "SELECTED");%>> 5 Tickets </ option> 
  <Option value = "6 Tickets" <% if (resubmit.getNumTickets () == "6 Tickets") out.println ( "SELECTED");%>> 6 Tickets </ option> 
  <Option value = "7 Tickets" <% if (resubmit.getNumTickets () == "7 Tickets") out.println ( "SELECTED");%>> 7 Tickets </ option> 
  <Option value = "8 Tickets" <% if (resubmit.getNumTickets () == "8 Tickets") out.println ( "SELECTED");%>> 8 Tickets </ option> 
  <Option value = "9 Tickets" <% if (resubmit.getNumTickets () == "Tickets 9") out.println ( "SELECTED");%>> 9 Tickets </ option> 
  </ Select> 
  </ Td> 
  </ Tr> 
<tr>
  <td> Stadium Level: </ td> 
<td>
  <select Name="stadiumTier"> 
  <option Value="Please enter a Stadium Tier"> Please enter a Stadium Tier </ option> 
  <Option value = "Tier A" <% if (resubmit.getStadiumTier () == "Tier A") out.println ( "SELECTED");%>> Tier A </ option> 
  <Option value = "Tier B" <% if (resubmit.getStadiumTier () == "Tier B") out.println ( "SELECTED");%>> Tier B </ option> 
  <Option value = "Tier C" <% if (resubmit.getStadiumTier () == "Tier C") out.println ( "SELECTED");%>> Tier C </ option> 
  <Option value = "Tier D" <% if (resubmit.getStadiumTier () == "Tier D") out.println ( "SELECTED");%>> Tier D </ option> 
  <Option value = "Tier E" <% if (resubmit.getStadiumTier () == "Tier E") out.println ( "SELECTED");%>> Tier E </ option> 
  </ Select> 
  </ Td> 
  </ Tr> 
<tr>
  <td> Votes Price: </ td> 
<td>
  <select Name="ticketPrice"> 
  <option Value="Please enter a Ticket Price"> Please enter a Ticket Price </ option> 
  <Option value = "$ 12" <% if (resubmit.getTicketPrice () == "$ 12") out.println ( "SELECTED");%>> $ 12 </ option> 
  <Option value = "$ 17" <% if (resubmit.getTicketPrice () == "$ 17") out.println ( "SELECTED");%>> $ 17 </ option> 
  <Option value = "$ 25" <% if (resubmit.getTicketPrice () == "$ 25") out.println ( "SELECTED");%>> $ 25 </ option> 
  <Option value = "$ 35" <% if (resubmit.getTicketPrice () == "$ 35") out.println ( "SELECTED");%>> $ 35 </ option> 
  <Option value = "$ 50" <% if (resubmit.getTicketPrice () == "$ 50") out.println ( "SELECTED");%>> $ 50 </ option> 
  </ Select> 
  </ Td> 
  </ Tr> 
<tr>
  <td Colspan="2" align="center"> <input TYPE="submit" VALUE="Submit"> </ td> 
  </ Tr> 
  </ Table> 


  </ Form> 

  </ Body> 


  Below are dealing with the form ControllerServlet.java 

  Package org.javapitfalls.item36; 

  Import java.io. *; 
  Import java.util .*; 
  Import javax.servlet .*; 
  Import javax.servlet.http .*; 
  Import org.javapitfalls.item36 .*; 

  Public class ControllerServlet extends HttpServlet ( 

  Private static String SESSION_ID; 

  Public void destroy () () 

  Public void init () (throws ServletException 

  SESSION_ID = getInitParameter ( "id"); 
  ) 

  Protected void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException ( 

  Process (req, res); 

  ) 

  Protected void process (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException ( 

  HttpSession session = req.getSession (false); 
  String numTickets = req.getParameter ( "numTickets"); 
  String stadiumTier = req.getParameter ( "stadiumTier"); 
  String ticketPrice = req.getParameter ( "ticketPrice"); 

  If (session == null) ( 

  If ((numTickets == null) | | (stadiumTier == null) | | (ticketPrice == null)) ( 

  GetServletConfig (). GetServletContext (). GetNamedDispatcher ( "form"). Forward (req, res); 

  Else () 

  Throw new ServletException ( "[form] Page Not Found"); 

  ) 

  Else () 

  If ((! NumTickets.equals ( "Please enter a Ticket #")) & & 
  (! StadiumTier.equals ( "Please enter a Stadium Tier")) & & (! TicketPrice.equals ( "Please enter a Ticket Price"))) ( 

  String sessionValidatorID = (String) session.getAttribute (SESSION_ID); 
  If (sessionValidatorID! = Null) (/ / the first time to deal with 
  Session.removeAttribute (SESSION_ID); 
  GetServletConfig (). GetServletContext (). GetNamedDispatcher ( "success"). Forward (req, res); 

  ) Else (/ / handle to repeat 
  GetServletConfig (). GetServletContext (). GetNamedDispatcher ( "resubmit"). Forward (req, res); 

  ) 
  Else () 

  GetServletConfig (). GetServletContext (). GetNamedDispatcher ( "form"). Forward (req, res); 

  ) 

  ) 
  ) 

  ) 
  Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply