Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

"Online Tweeter Enterprise Application" : Creating a Servlet named DisplayTweets in NetBeans Web module - Part 9

Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create a Servlets in Web module by name "DisplayTweets.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 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 another 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 DisplayTweets.java. It has code which is generated by NetBeans. Just add or remove code given below.




package com.hubberspot.servlets;

import com.hubberspot.ejb.ActiveFollowersOnline;
import com.hubberspot.ejb.Tweet;
import com.hubberspot.ejb.TweetsFinder;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
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;
import javax.servlet.http.HttpSession;

@WebServlet(name = "DisplayTweets", urlPatterns = {"/DisplayTweets"})
public class DisplayTweets extends HttpServlet {

    @EJB
    private TweetsFinder tweetsFinder;
    
    @EJB
    private ActiveFollowersOnline activeFollowersOnline;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        request.getSession(true);

        PrintWriter out = response.getWriter();

        try {
            out.println("");
            out.println("");
            out.println("");
            out.println("Latest Tweets");
            out.println("");
            out.println("");
            out.println("");
            out.println("
");
            out.println("");
            out.println("");
            out.println("
");
            out.println("
"); out.println("Latest Tweets"); out.println(""); out.println("Add new tweets"); out.println(" | "); out.println(activeFollowersOnline.getFollowersOnline() + " follower(s) reading tweets."); out.println("
"); out.println("
"); List tweets = tweetsFinder.findAllTweets(); for (Iterator it = tweets.iterator(); it.hasNext();) { Tweet tweet = (Tweet) it.next(); out.println(""); out.println(" "); out.println(""); out.println(" "); out.println(" "); out.println(""); out.println(" "); out.println("
"); out.println("User: " + tweet.getUsername() + ""); out.println("
"); out.println("Tweeted: " + tweet.getTweet() + ""); out.println("
"); out.println("
"); } out.println(""); out.println(""); } finally { out.close(); } } @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 10) you will learn how to build and run the application in NetBeans.


 
© 2021 Learn Java by Examples Template by Hubberspot