Skip to content

Instantly share code, notes, and snippets.

@TheB1ackSheep
Created May 13, 2015 12:54
Show Gist options
  • Save TheB1ackSheep/008a436ede7b0cec1365 to your computer and use it in GitHub Desktop.
Save TheB1ackSheep/008a436ede7b0cec1365 to your computer and use it in GitHub Desktop.
Java Bean Example
package model;
import java.util.List;
public class Customer {
private String name;
private String email;
private String password;
private String[] qoute;
private boolean isAdmin;
public String[] getQoute() {
return qoute;
}
public String getQoute(int index){
return this.qoute[index];
}
public void setQoute(String[] qoute) {
this.qoute = qoute;
}
public void setQoute(int index,String qoute){
this.qoute[index] = qoute;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isIsAdmin() {
return isAdmin;
}
public void setIsAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
}
package controller.customer;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "CustomerController", urlPatterns = {"/customer"})
public class CustomerController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getServletContext().getRequestDispatcher("/WEB-INF/customer/index.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getServletContext().getRequestDispatcher("/WEB-INF/customer/process.jsp").forward(request, response);
}
}
<form action="${pageContext.request.contextPath}/customer" method="POST">
<input type="text" name="name" placeholder="Your name"/>
<input type="text" name="email" placeholder="email"/>
<input type="password" name="password" placeholder="password"/>
isAdmin : <input type="radio" name="isAdmin" value="true"/>Yes
<input type="radio" name="isAdmin" value="false"/>No<br/>
<input type="test" name="qoute" placeholder="Qoute 1"/>
<input type="test" name="qoute" placeholder="Qoute 2"/>
<input type="test" name="qoute" placeholder="Qoute 3"/>
<button>Submit</button>
</form>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<jsp:useBean id="cust" class="model.Customer"/>
<jsp:setProperty name="cust" property="*"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello, ${cust.name}</h1>
Your email is ${cust.email}<br/>
You is ${cust.isAdmin ? "" : "not"} Admin
<pre><c:forEach var="i" begin="0" end="${fn:length(cust.qoute)-1}">${cust.qoute[i]}<br></c:forEach></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment