Skip to content

Instantly share code, notes, and snippets.

@TheB1ackSheep
Created May 14, 2015 04:35
Show Gist options
  • Save TheB1ackSheep/440528ae34a52d66a28b to your computer and use it in GitHub Desktop.
Save TheB1ackSheep/440528ae34a52d66a28b to your computer and use it in GitHub Desktop.
Java Bean with Custom Tag
<%--
Created by IntelliJ IDEA.
User: Falook Glico
Date: 5/14/2015
Time: 10:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Student Form</title>
</head>
<body>
<h1>Student</h1>
<form method="POST">
<label for="name">Name :</label><br/>
<input type="text" name="name" id="name" placeholder="name"/><br/>
<label for="email">Email :</label><br/>
<input type="email" name="email" id="email" placeholder="email"/><br/>
<label>Are you gradated ?</label><br/>
Yes : <input type="radio" name="graduated" value="true"/>
No : <input type="radio" name="graduated" value="false"/><br/>
<button>submit</button>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="sit.int303.student" prefix="stud"%>
<jsp:useBean id="student" class="model.Student"/>
<jsp:setProperty name="student" property="*"/>
<html>
<head>
<title>Student Info</title>
</head>
<body>
<stud:student student="${student}"/>
</body>
</html>
package model;
import java.util.List;
/**
* Created by Falook Glico on 5/14/2015.
*/
public class Student {
private String name;
private String email;
private boolean graduated;
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 boolean isGraduated() {
return graduated;
}
public void setGraduated(boolean graduated) {
this.graduated = graduated;
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>student</short-name>
<uri>sit.int303.student</uri>
<tag>
<name>student</name>
<tag-class>tag.StudentTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>student</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
package controller;
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 java.io.IOException;
/**
* Created by Falook Glico on 5/14/2015.
*/
@WebServlet(name="StudentController", urlPatterns = "/student")
public class StudentController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getServletContext().getRequestDispatcher("/WEB-INF/view/student/index.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getServletContext().getRequestDispatcher("/WEB-INF/view/student/process.jsp").forward(req,resp);
}
}
package tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.List;
/**
* Created by Falook Glico on 5/14/2015.
*/
public class StudentTag extends SimpleTagSupport {
private model.Student student;
public model.Student getStudent() {
return student;
}
public void setStudent(model.Student student) {
this.student = student;
}
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
if(this.student != null){
out.print("name : "+student.getName());
out.print("<br/>");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment