i am a beginner here, now i am having trouble adding function to the buttons.(i have no idea what to do so might need deep explaination on everything)
i totally don't know how to add function onto the button.
here is my code:
Second part is servlet
<%@page import="ejb.CartServlet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
function appleFunction()
{
alert("Apple is added!");
}
function grapeFunction()
{
alert("Grape is added!");
}
function peachFunction()
{
alert("Peach is added!");
}
function kiwiFunction()
{
alert("Kiwi is added!");
}
</script>
</head>
<body>
<h1>Fruits Corder</h1>
<h6>Customer Name:</h6>
<form action="CartServlet" method="post">
<input type ="text" name="customerName1">
<input type="button" name="login" value="Login" onclick=""></input>
<h5>Adding fruits to cartBean for a customer</h5>
<table>
<th>Item</th>
<th>Item Name</th>
<tr>
<td><img src="apple.jpg" alt="applePic" width="100" height="100"></td>
<td><input type="text" name="appleName" value="Apple"></td>
<td><input type="button" name="addbut1" value="Add" onclick=""></td>
<td><input type="button" name="remove1" value="Remove" onclick=""></td>
</tr>
<tr>
<td><img src="grape.png" alt="grapePic" width="100" height="100"></td>
<td><input type="text" name="grapeName" value="Grape"></td>
<td><input type="button" name="addbut2" value="Add"></td>
<td><input type="button" name="remove2" value="Remove" onclick=""></td>
</tr>
<tr>
<td><img src="peach.jpg" alt="peachPic" width="100" height="100"></td>
<td><input type="text" name="peachName" value="Peach"></td>
<td><input type="button" name="addbut3" value="Add"></td>
<td><input type="button" name="remove3" value="Remove" onclick=""></td>
</tr>
<tr>
<td><img src="kiwi.jpg" alt="kiwiPic" width="100" height="100"></td>
<td><input type="text" name="kiwiName" value="Kiwi"></td>
<td><input type="button" name="addbut4" value="Add"></td>
<td><input type="button" name="remove4" value="Remove" onclick=""></td>
</tr>
</table>
<h1>Cart List</h1>
<input type="submit" name="update" value="Update">
<input type="button" name="refresh" value="Refresh">
</form>
</body>
</html>
package ejb;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ejb.EJB;
/**
*
* @author chiewwei
*/
@WebServlet(name = "CartServlet", urlPatterns = {"/CartServlet"})
public class CartServlet extends HttpServlet {
@EJB
private CartBeanRemote myCartBean;
// CartBeanRemote cartBean = lookupCartBeanRemote();
public void login(){
myCartBean.initialize("ken", "189");
}
public void addApple(HttpServletRequest request,HttpServletResponse response){
String name = request.getParameter("appleName");
myCartBean.addItem(name);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name1 = request.getParameter("grapeName");
String name2 = request.getParameter("peachName");
String name3 = request.getParameter("kiwiName");
myCartBean.addItem(name1);
myCartBean.addItem(name2);
myCartBean.addItem(name3);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet CartServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>" + myCartBean.getItems() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
/** private CartBeanRemote lookupCartBeanRemote() {
try {
Context c = new InitialContext();
return (CartBeanRemote) c.lookup("java:global/EJBCartApp/EJBCartApp-ejb/CartBean!ejb.CartBeanRemote");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}*/
}