Skip to content

Instantly share code, notes, and snippets.

@mathieuancelin
Created April 8, 2012 16:00
Show Gist options
  • Save mathieuancelin/2338154 to your computer and use it in GitHub Desktop.
Save mathieuancelin/2338154 to your computer and use it in GitHub Desktop.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a href="index">Back</a>
<ul>
<c:forEach items="${shortens}" var="shorten">
<li><a href="${shorten.url}">${shorten.url}</a> => <a href="http://localhost:8080/short/r/${shorten.id}">http://localhost:8080/short/r/${shorten.id}</a> : <a href="delete?id=${shorten.id}">X</a></li>
</c:forEach>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a href="all">All</a>
<form action="index" method="post">
<input type="text" name="url" id="url">
<input type="submit" value="add">
</form>
<c:if test="${not empty miniurl}">
<p>
<a href="${url}">${url}</a> => <a href="${miniurl}">${miniurl}</a>
</p>
</c:if>
</body>
</html>
@Stateless
public class ShortenService {
@PersistenceContext EntityManager em;
public List<Shorten> all() {
return em.createQuery("select o from Shorten o").getResultList();
}
public long count() {
Long l = Long.parseLong(em.createQuery("select count(e) from Shorten e").getSingleResult().toString());
if (l == null || l < 0) {
return 0L;
}
return l;
}
public int deleteAll() {
return em.createQuery("delete from Shorten").executeUpdate();
}
public void delete(Shorten shorten) {
em.remove(findById(shorten.getId()));
}
public Shorten findById(Long primaryKey) {
return em.find(Shorten.class, primaryKey);
}
public Shorten save(Shorten shorten) {
if (em.contains(shorten)) {
return em.merge(shorten);
}
em.persist(shorten);
return shorten;
}
public Shorten get(String val) {
Shorten shorten = (Shorten) em.createQuery("select e from Shorten e where e.url == " + val).getSingleResult();
if (shorten == null || shorten.getUrl() == null) {
shorten = new Shorten(val);
save(shorten);
}
return shorten;
}
}
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Shorten implements Serializable {
private final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String url;
public Shorten() {}
public Shorten(String url) {
this.url = url;
}
public String completeUrl() {
return "/r/" + id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
import foo.bar.services.ShortenService;
import foo.bar.model.Shorten;
import java.io.IOException;
import javax.ejb.EJB;
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="Application", urlPatterns={"/index"})
public class Application extends HttpServlet {
@EJB ShortenService handler;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String url = req.getParameter("url");
Shorten shorten = handler.save(new Shorten(url));
req.setAttribute("url", url);
req.setAttribute("miniurl", "http://localhost:8080/short/r/" + shorten.getId());
getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment