com.astech.mnlottery.web.SearchByName.java Source code

Java tutorial

Introduction

Here is the source code for com.astech.mnlottery.web.SearchByName.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.astech.mnlottery.web;

import com.astech.mnlottery.service.model.LotteryGameResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author franzsilv1
 */
public class SearchByName extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SearchByName</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SearchByName at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setAttribute("navBarContent", Builders.buildNavBar(0));
        request.setAttribute("homeNavBar", Builders.buildHomeNavBar(1));

        request.getRequestDispatcher("./searchByName.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("navBarContent", Builders.buildNavBar(0));
        request.setAttribute("homeNavBar", Builders.buildHomeNavBar(1));

        String searchString = request.getParameter("searchString");
        String fetchUrl = "http://localhost:8080/AstonTech.MNLottery.RestService/webresources/lotGame/get/byName/";
        fetchUrl = fetchUrl + searchString;
        Collection<LotteryGameResponse> matchingGames = null;
        try {
            URLConnection urlConnection = new URL(fetchUrl).openConnection();
            urlConnection.connect();
            JsonReader reader = new JsonReader(new InputStreamReader(urlConnection.getInputStream()));
            Gson gson = new GsonBuilder().create();
            Type type = new TypeToken<Collection<LotteryGameResponse>>() {
            }.getType();
            matchingGames = gson.fromJson(reader, type);
        } catch (IOException | JsonIOException | JsonSyntaxException ex) {
            System.out.println("Exception caught: " + ex);
        }

        request.setAttribute("matchingGames", matchingGames);

        request.getRequestDispatcher("./searchByName.jsp").forward(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}