Skip to content

Commit dfa0ded

Browse files
committed
0 parents  commit dfa0ded

7 files changed

Lines changed: 162 additions & 0 deletions

File tree

pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

src/main/webapp/WEB-INF/web.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<session-config>
2+
<tracking-mode>COOKIE</tracking-mode>
3+
</session-config>

src/main/webapp/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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>

src/main/webapp/result.jsp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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>

0 commit comments

Comments
 (0)