File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.sample</groupId >
8+ <artifactId >LiquorStoreApp</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+ <packaging >war</packaging >
11+
12+ <dependencies >
13+ <dependency >
14+ <groupId >javax.servlet</groupId >
15+ <artifactId >javax.servlet-api</artifactId >
16+ <version >3.0.1</version >
17+ <scope >provided</scope >
18+ </dependency >
19+ </dependencies >
20+
21+ <build >
22+ <finalName >SampleServlet</finalName >
23+ <plugins >
24+ <plugin >
25+ <groupId >org.apache.tomcat.maven</groupId >
26+ <artifactId >tomcat7-maven-plugin</artifactId >
27+ <version >2.2</version >
28+ </plugin >
29+ </plugins >
30+ </build >
31+ </project >
32+
Original file line number Diff line number Diff line change 1+ package com .sample ;
2+
3+ import com .sample .model .LiquorType ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
8+ /**
9+ * Created by kasun on 5/24/17.
10+ */
11+ public class LiquorService {
12+
13+ public List getAvailableBrands (LiquorType type ){
14+
15+ List brands = new ArrayList ();
16+
17+ if (type .equals (LiquorType .WINE )){
18+ brands .add ("Adrianna Vineyard" );
19+ brands .add (("J. P. Chenet" ));
20+
21+ }else if (type .equals (LiquorType .WHISKY )){
22+ brands .add ("Glenfiddich" );
23+ brands .add ("Johnnie Walker" );
24+
25+ }else if (type .equals (LiquorType .BEER )){
26+ brands .add ("Corona" );
27+
28+ }else {
29+ brands .add ("No Brand Available" );
30+ }
31+ return brands ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .sample ;
2+
3+ import com .sample .model .LiquorType ;
4+
5+ import javax .servlet .RequestDispatcher ;
6+ import javax .servlet .ServletException ;
7+ import javax .servlet .annotation .WebServlet ;
8+ import javax .servlet .http .HttpServlet ;
9+ import javax .servlet .http .HttpServletRequest ;
10+ import javax .servlet .http .HttpServletResponse ;
11+ import java .io .IOException ;
12+ import java .util .List ;
13+
14+
15+ @ WebServlet (
16+ name = "selectliquorservlet" ,
17+ urlPatterns = "/SelectLiquor"
18+ )
19+ public class SelectLiquorServlet extends HttpServlet {
20+
21+ @ Override
22+ protected void doPost (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
23+
24+ String liquorType = req .getParameter ("Type" );
25+
26+ LiquorService liquorService = new LiquorService ();
27+ LiquorType l = LiquorType .valueOf (liquorType );
28+
29+ List liquorBrands = liquorService .getAvailableBrands (l );
30+
31+ req .setAttribute ("brands" , liquorBrands );
32+ RequestDispatcher view = req .getRequestDispatcher ("result.jsp" );
33+ view .forward (req , resp );
34+
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package com .sample .model ;
2+
3+ /**
4+ * Created by kasun on 5/24/17.
5+ */
6+ public enum LiquorType {
7+ WINE ,BEER ,WHISKY
8+
9+ }
Original file line number Diff line number Diff line change 1+ <session-config >
2+ <tracking-mode >COOKIE</tracking-mode >
3+ </session-config >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Liquor Store</ title >
6+ </ head >
7+ < body >
8+ < center >
9+ < h1 >
10+ Select the type of Liquor
11+ </ h1 >
12+ < form method ="post " action ="SelectLiquor ">
13+ < br >
14+ < select name ="Type " size ="1 ">
15+ < option > WINE</ option >
16+ < option > WHISKY</ option >
17+ < option > BEER</ option >
18+
19+ </ select >
20+ < br > < br >
21+ < input type ="submit ">
22+ </ form >
23+ </ center >
24+
25+
26+ </ body >
27+ </ html >
Original file line number Diff line number Diff line change 1+ <%@ page import ="java.util.*" %>
2+ <!DOCTYPE html>
3+ <html >
4+ <body >
5+ <center >
6+ <h1 >
7+ Available Brands
8+ </h1 >
9+ <%
10+ List result= (List ) request. getAttribute(" brands" );
11+ Iterator it = result. iterator();
12+ out. println(" <br>We have <br><br>" );
13+
14+ while (it. hasNext()){
15+
16+ out. println(it. next()+ " <br>" );
17+
18+ }
19+
20+ % >
21+ </body >
22+ </html >
You can’t perform that action at this time.
0 commit comments