Created
September 21, 2014 00:13
-
-
Save lighta971/7da33c41eb4101945c0b to your computer and use it in GitHub Desktop.
intech java: create article exo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.intechinfo.s5.jee.servletJsp; | |
import java.io.IOException; | |
import java.util.Date; | |
import javax.annotation.Resource; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityTransaction; | |
import javax.persistence.PersistenceContext; | |
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.transaction.HeuristicMixedException; | |
import javax.transaction.HeuristicRollbackException; | |
import javax.transaction.NotSupportedException; | |
import javax.transaction.RollbackException; | |
import javax.transaction.SystemException; | |
import javax.transaction.Transaction; | |
import javax.transaction.UserTransaction; | |
import fr.intechinfo.s5.jee.jpa.Article; | |
/** | |
* Servlet implementation class CreateArticleForm | |
*/ | |
@WebServlet("/create-article-form") | |
public class CreateArticleForm extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
@PersistenceContext(unitName="BlogPU") | |
EntityManager entityManager; | |
@Resource | |
private UserTransaction utx; | |
/** | |
* @see HttpServlet#HttpServlet() | |
*/ | |
public CreateArticleForm() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
/** | |
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) | |
*/ | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
request.getRequestDispatcher("/createArticleForm.jsp") | |
.forward(request, response); | |
} | |
/** | |
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) | |
*/ | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
String title = request.getParameter("title"); | |
String content = request.getParameter("content"); | |
try { | |
utx.begin(); | |
} catch (NotSupportedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (SystemException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
Article article = new Article(); | |
article.setTitle(title); | |
article.setContent(content); | |
article.setCreatedOn(new Date()); | |
entityManager.persist(article); | |
try { | |
utx.commit(); | |
} catch (SecurityException | IllegalStateException | RollbackException | |
| HeuristicMixedException | HeuristicRollbackException | |
| SystemException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
response.sendRedirect("article-list"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment