简体   繁体   中英

How to receive info to put in HTML/JSP from post servlet

Basically my goal for this page I'm working on is for users to type in a stock symbol and this information goes to a post method and send back the data to put on the same html/jsp page. I have been able to get this to work where the form leads to another JSP page, but that has to be a separate page, I'd like to be able to stay on the same page and have the info come up. If you have a resource that could teach me how to deal with this problem, I would appreciate that just as much as a solution. I have been using the Gradle Build Tool.

Here is the form(in index.jsp):

<h1>Search Stock</h1>
        <form method="POST" action="DataPage.jsp">
                <input type = "text" name = "Symbol">
                <input type = "submit" name = "getData">
        </form>

Here is the functioning JSP code(DataPage.jsp):

  <%
   String Ticker = request.getParameter("Symbol");
   PrintWriter write = response.getWriter();
   if((Ticker == null)){
    String message = "Please enter a stock symbol";
    write.println(message);

   }else{
    try{
      Company object = Serializing.getCompany(Ticker);
      object.updateData();
      write.println("data last added" + object.getLastUpdate());
      write.println(object.getSentiment());
    }catch(NullPointerException x){
      Company object = Serializing.getCompany(Ticker);
   }


  }%>

Here is the servlet I tried writing(DataServlet.java), I have very little experience with servlets, I scavenged this from different sources and questions on stackoverflow:

package Default;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Ceyer on 9/3/2015.
 */

@javax.servlet.annotation.WebServlet(name = "DataServlet", urlPatterns = ("/"))
public class DataServlet extends javax.servlet.http.HttpServlet {

    private static final long serialVersionUID = 1L;

    public DataServlet() {
        super();

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

    }
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String Ticker = request.getParameter("Symbol");
        if ((Ticker == null)||Ticker.trim().isEmpty()) {
            String message = "Please enter a stock symbol";
            request.setAttribute("data", message);
            getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
        } else {

            PrintWriter write = response.getWriter();
            try {
                Company object = Serializing.getCompany(Ticker);
                object.updateData();
                request.setAttribute("data", object.getSentiment() + "updated last" + object.getLastUpdate());
                getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
            } catch (NullPointerException x) {
                Company object = Serializing.getCompany(Ticker);
                request.setAttribute("data", "We do not have info on this stock");
                getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
}

If you want to use only one page and with a servlet, I think you can use session and response.sendRedirect() to do it.

This is index.jsp page

 <h1>Search Stock</h1>
    <form method="POST" action="DataServlet" onsubmit="dataCheck()">
        <input type="text" name="Symbol"> 
        <input type="submit" value="getData">
    </form>

    <%
        if(session.getAttribute("data") != null) {
            out.print("<p>" + session.getAttribute("data"));
            session.removeAttribute("data");
        } 
    %>

    <script>
        function dataCheck() {
            if(document.getElementsByName[0].value == ""){
                alert("Symbol is null!");
                return false;
            }
            return true;
        }
    </script>

This is DataServlet class

public class DataServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String Ticker = request.getParameter("Symbol");
        Company object = Serializing.getCompany(Ticker);

        if (object != null) {
            object.updateData();
            request.getSession().setAttribute("data", object.getSentiment() + 
                    "updated last" + object.getLastUpdate());
        } else {
            request.getSession().setAttribute("data", "We do not have info on this stock");
        }
        response.sendRedirect("index.jsp");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM