Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create a Servlets in Web module by name "TweetsSubmit.java". A Servlet is a server side program which executes at the server producing dynamic content for the web applications.
Step 1: Open "Tweeter-war" project and right click Source Packages and then select New and than Other as shown in fig below:
Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Web and in the File Types: select Servlet as shown in fig below.
Step 1: Open "Tweeter-war" project and right click Source Packages and then select New and than Other as shown in fig below:
Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Web and in the File Types: select Servlet as shown in fig below.
Step 3: Click
.
New Servlet dialog box gets open. It prompts us to enter Class Name: , Project: , Location: , Package: and Created File: etc. Enter the values as shown in the fig below.
Step 4: Click
.
yet New Servlet dialog box gets open. It prompts us to enter values related to Servlet deployment. Do not check "Add information to deployment descriptor (web.xml)". Just enter values as shown in figure below.
and than just click
A new servlet gets created in the source packages by name TweetsSubmit.java. It has code which is generated by NetBeans. Just add or remove code given below.
package com.hubberspot.servlets; import com.hubberspot.ejb.Tweet; import java.io.IOException; import java.io.PrintWriter; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Resource; import javax.jms.*; 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 = "TweetsSubmit", urlPatterns = {"/TweetsSubmit"}) public class TweetsSubmit extends HttpServlet { @Resource(mappedName = "jms/tweetsFactory") private ConnectionFactory connectionFactory; @Resource(mappedName = "jms/tweets") private Queue queue; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("txtUsername"); String tweet = request.getParameter("txtTweet"); if (username != null && tweet != null) { try { Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session.createProducer(queue); ObjectMessage objectMessage = session.createObjectMessage(); Tweet newTweet = new Tweet(); newTweet.setUsername(username); newTweet.setTweet(tweet); objectMessage.setObject(newTweet); messageProducer.send(objectMessage); connection.close(); response.sendRedirect("DisplayTweets"); } catch (JMSException ex) { Logger.getLogger(TweetsSubmit.class.getName()).log(Level.SEVERE, null, ex); } } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Short description"; } }
In the next section of this blog (part 9) you will learn how to create a yet another Servlet in NetBeans by name DisplayTweets, for this application in the Web module.