简体   繁体   中英

CDI - Java EE Servlet saving variables to a managed bean

I have a Java EE class that currently reads info from a form and prints it out.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Response extends HttpServlet
{
    String date = "0";

    public void init() throws ServletException 
    {
        //Get Election Date from xml
        String initial = getInitParameter("electionDate");
        date = initial;
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        //Get values from form
        PrintWriter out = response.getWriter(); 
        String firstName=request.getParameter("firstname");  
        String lastName=request.getParameter("lastname");  
        String address=request.getParameter("address");  
        String city=request.getParameter("city");  
        String state=request.getParameter("state");  
        String zip=request.getParameter("zip");  
        String phone = request.getParameter("phone"); 
        String affil=request.getParameter("affil");

        //Print Summary of Voter Registration
        out.println("<html>");
        out.println("<head><title>Registration Summary</title></head>");
        out.println("<body>");
        out.println("<p>Registration Summmary</p>"); 
        out.println("<p>First Name: " + firstName + "</p>");
        out.println("<p>Last Name: " + lastName + "</p>"); 
        out.println("<p>Address : " + address + "</p>"); 
        out.println("<p>City : " + city + "</p>");
        out.println("<p>State : " + state + "</p>");
        out.println("<p>Zip: " + zip + "</p>");
        out.println("<p>Phone Number: " + phone + "</p>"); 
        out.println("<p>Affiliation: " + affil + "</p>"); 
        out.println("<p>Next Election Date: " + date + "</p></p>");

        out.println("<p>Is the above information correct?</p>"); 
        out.println("<button>Yes</button>"); 
        out.println("<button>No</button>"); 

        out.println("</body></html>");
        out.close();
    }
}

I want to get the values (firstName, lastName, etc.) from this Java servlet and inject to a bean.

Then when this file calls another servlet I want the values from the bean to be available in that servlet.

I just want to know how to store the variables I created above into a managed bean and then have the other servlet reference and retrieve the variables in that bean.

I have beans.xml, web.xml, pom.xml (I'm using Maven) files set up already.

You cannot simply inject Strings, so you will have to use a qualifier (the simplest one is @Named, see if that is sufficient).

In your servlet, say

@Produces
@Named("foo")
String lastName;
...
void doPost() {
  lastName = getParameter(...);
}

and in the target bean, use

@Inject
@Named("foo")
String lastName;

Since you are in a Request-Scope, keep in mind that injecting request-scoped values into longer living instances (EJBs for example) might lead to unpredictable behavior. I seriously doubt that your approach will make you happy. Perhaps you could tell us more about what you are trying to do?

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