Beruflich Dokumente
Kultur Dokumente
Step 1:
At the Net Bean Startup
Select File->New Project
In the next dialog box select the server Glassfish V3 then press Next
In the next dialog box select the Struts2 Framework then press Finish
You will see a default page HelloWorld.jsp and the directory structure like this:
Step2:
In the Directory Structure click on Source Package and then Right click on the
example and select New, click on the Java Class from the drop down menu.
Name your class name and choose the location to be source package and
click Finish.
import java.sql.SQLException;
import javax.servlet.http.*;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public class ShowAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware
{
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest( HttpServletRequest request)
{
this.request=request;
}
public void setServletResponse( HttpServletResponse response)
{
this.response=response;
}
public String execute() throws SQLException
{
ProductDataBean pdb=new ProductDataBean();
ArrayList al=pdb.getProductList();
request.setAttribute("productlist",al);
return SUCCESS;
}
}
Like
the
above
ShowAction.java
add
the
ShoppingCart.java,
RemoveItemAction.java,
ProductDataBean.java,
DVD.java,
CheckoutAction.java, AddToShoppingCartAction.java to the Source Package.
//ShoppingCart.java
import java.util.*;
import java.sql.*;
public ShoppingCart() {
items = new Vector();
}
if (items.size() == 0) {
items.addElement(newItem);
return;
}
for (int i = 0; i< items.size(); i++) {
DVD dvd = (DVD) items.elementAt(i);
if (dvd.getMovie().equals(newItem.getMovie())) {
dvd.setQuantity(dvd.getQuantity()+newItem.getQuantity());
items.setElementAt(dvd,i);
flag = true;
break;
}
}
if (newItem.getQuantity()>0 && (flag == false)) {
items.addElement(newItem);
}
}
connection = ProductDataBean.getConnection();
statement = connection.createStatement();
while (e.hasMoreElements()) {
DVD item = (DVD) e.nextElement();
String itemQuantity = "" + item.getQuantity();
totalPrice = totalPrice + item.getPrice() *
Integer.parseInt(itemQuantity);
Enumeration e = items.elements();
connection = ProductDataBean.getConnection();
statement = connection.createStatement();
while (e.hasMoreElements()) {
DVD item = (DVD) e.nextElement();
String movieName = item.getMovie();
int qt =item.getQuantity();
ResultSet srs = statement.executeQuery("select quantity from products
"+
"where movie = '"+movieName+"'");
srs.next();
srs.updateInt("quantity", qt);
srs.updateRow();
}
}
//RemoveItemAction.java
import javax.servlet.http.*;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
ActionSupport
implements
{
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest( HttpServletRequest request)
{
this.request=request;
}
public void setServletResponse( HttpServletResponse response)
{
this.response=response;
}
public String execute()
{
// Get the index of the item to remove
int itemIndex = Integer.parseInt(request.getParameter("item"));
HttpSession session = request.getSession();
cart.removeItem(itemIndex);
/**
String url="/jsp/ShowProductCatalog.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(request, response);**/
return SUCCESS;
}
}
//ProductDataBean.java
import java.io.*;
import java.sql.*;
import java.util.*;
public ProductDataBean() {
try {
String userName = "sample";
String password = "sample";
//
connection
=
DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:XE", userName, password);
import java.io.*;
}
public String getMovie() {
return m_movie;
}
public void setRating(String rating) {
m_rated = rating;
}
public String getRating() {
return m_rated;
}
public void setYear(String year) {
m_year = year;
}
public String getYear() {
return m_year;
}
public void setPrice(double p) {
m_price = p;
}
public double getPrice() {
return m_price;
}
public void setQuantity(int q) {
quantity = q;
}
public int getQuantity() {
return quantity;
}
}
//CheckoutAction.java
import javax.servlet.http.*;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public
class
CheckoutAction
extends
ServletRequestAware,ServletResponseAware
ActionSupport
implements
{
private HttpServletRequest request;
private HttpServletResponse response;
cart.refreshoriginaldata();
} catch(Exception e){
e.printStackTrace();}
// response.sendRedirect(response.encodeRedirectURL(
//
"ShowConfirmation.jsp"));
return SUCCESS;
}
}
//AddToShoppingCartAction.java
import javax.servlet.http.*;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public class AddToShoppingCartAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware
{
private HttpServletRequest request;
private HttpServletResponse response;
}
public void setServletResponse( HttpServletResponse response)
{
this.response=response;
}
public String execute()
{
cart.addItem(DVDItem);
/** String url="ShowProductCatalog.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(request, response);**/
session.setAttribute("Shoppingcart",cart);
return SUCCESS;
}
}
Now In the Directory Structure click on Web Pages and then Right click on the
example and select New, click on the JSP from the drop down menu.
Name your JSP File and choose the location to be Web Pages and the folder to
be example and click Finish.
//displayshoppingcart.jsp
<%-- DisplayShoppingCart.jsp --%>
<%@ page contentType="text/html;charset=UTF-8"language="java"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ page import="example.*,java.util.*,java.text.*" %>
session.getAttribute("ShoppingCart");
if (cart == null){
cart = new ShoppingCart();
session.setAttribute("ShoppingCart", cart);
}
if (items.size() != 0) {
%>
<%-- Display the heading of the shoppingCart --%>
<h1>Shopping Cart</h1>
<br>
<table border=4>
<tr><th>DVD Names<th>Rate<th>Year<th>Price<th>Quantity
<th>Remove
<%
method="POST">
<td><%= item.getMovie() %></td>
<td><%= item.getRating() %></td>
<td><%= item.getYear() %></td>
<td><%= item.getPrice() %></td>
<td><%= item.getQuantity() %></td>
<td>
<input type="hidden" name= "item" value='<%= i %>'/>
<input type="submit" value="Remove"/> </td>
</s:form>
</tr>
<%
%>
</table>
<s:form action="checkout"
method="POST">
<input type="submit" name="Submit" value="Check out">
</s:form>
<% }
%>
Like
the
above
jsp
page
add
the
showconfirmation.jsp,
showproductcatalog.jsp, and welcome.jsp. the code is listed below
<%-- ShowConfirmation.jsp --%>
<%-- ShowProductCatalog.jsp
--%>
<%-- Display all DVD products row by row on the table, add an addCart
button at the end of each row to allow clients to select
--%>
%>
</tbody>
</table>
<p><hr>
<%-- Display the current shopping Cart by including
DisplayShoppingCart.jsp
--%>
<jsp:include page="displayshoppingcart.jsp" flush="true" />
</center>
</body>
</html>
//Welcome.jsp
<%@ page contentType="text/html;charset=UTF-8"language="java"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>WELCOME TO DVD SHOPPING</title>
</head>
<body>
<h>Please click the button to show the Items...</h>
<s:form action = "show">
</s:form>
</body>
</html>
Inside the Databases Right Click on the Drivers and Select ->New Drivers
A Dialog box will open and in that add the executable .jar file from your PC
which are located in Oracle>Product>11.1.0(version)>db_1>jdbc>lib
Now in the Drivers you will see the New Driver which is Oracle Thin. Right
Click on the Oracle Thin and press Connect Using ..
Now fill up the Host to be localhost, Port to be 1521 and select service Id
and give your username and password in the respective columns and
check the show JDBC URL.
Now in the same dialog box click on the Advanced Tab which is beside Basic
Setting and select the Schema which you created from the drop down menu
and press ok.
Create a table:
Right Click on the Table to create a table
To View the inserted data Right Click on the Products Table and Click view
data
Now Create another Table which ShoppingCart in the same way as the above
table. The field values are shown in the below figure
Output:
Now Run the above project and you will get the output to be
After you click on the button the below page will display
Now select the Movie you like and enter the quantity in the respective field
and click on AddToCart and the cart will update like this.
Click on the Checkout button and the Confirmation page will come