java - Use a created object in servlet in JSP/forward object from servlet to JSP -


As the title, I am trying to use the same object created in the servlet in my JSP. Object is a model (called "customer", which holds value / data. I am setting the values ​​in the servlet and I am printing / receiving them in my JSP.

I have a JSP Trying to create a new object, but the value becomes zero.

Currently, variables and gate-management are stable in the customer class. It works, but I do not want them to be static. Exist in This object has to be taken and it must be re-used in my JSP.

Before someone says that I should read, trust me, I have not understood it correctly

servlet:

  string first name = request.getParameter ("foram"); ​​customer control = new customer (); control.setFirstName (first name); http session session = Request. Gate Session (); Session; SetAttivate ("Furname", First Name); Request .SetAttribute (" See "Request Distribution" = Request.GateRquest Dispatcher ("Resu lt.jsp"); See further (Request, Feedback);  

This is Customer Class:

  Private String String First Name; Public stable string getFirstName () {back firstName; } Public static zero setfirst-name (string first name) {Customer.firstName = firstName; }  

This is my JSP:

  & lt;% @ page language = "java" contentType = "text / html; charset = UTF-8 "PageEncoding =" UTF-8 "%> & Lt ;! DOCTYPE HTML PUBLIC "- // W3C // DTD HTML 4.01 Transcription // N" "http://www.w3.org/TR/html4/loose.dtd"> & Lt; Html & gt; & Lt; Top & gt; & Lt; Meta http-equiv = "content-type" content = "text / html; charset = UTF-8" & gt; & Lt; Title & gt; Your information in JSP format: & lt; / Title & gt; & Lt;% @ Page Import = "Abdial Customer"% & gt; & Lt;% HttpSessionListenerTest http = New HttpSessionListenerTest ();% & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; P & gt; & Lt; B & gt; Last name: & lt; / B & gt; & Lt;% = Customer.getFirstName () // I want to print it, without static methods% & gt; & Lt; / P & gt; & Lt; / Li & gt; & Lt; / Ul & gt;  

There is a problem with your customer class, The variable has been made a personal static and accessed in a public static manner. In your case, there will be only one first named variable, whether you create a number of client objects using a new customer () Static means a definition per class

Make your variable private in the customer class and Make public setters / gates to mutate the state. Once you do this, you can add them to your servlet as a feature in the request / session / application area, forward it to a JSP and access them with Expression Language (EL) or JSTL . Stop using scriptlets, they are 90

In your service,

  string first name = request.getParameter ("FORMAN"); Customer customer = new customer (); Customer.setFirstName (firstName); Http session session = request.getSession (); Session.setAttribute ("Customer", Customer);  

--- Forward to JSP ----

In your JSP, just use EL

  $ { Customer.firstName}  

In general, $ {yourAttributeName.yourPropertyNameFromPOJO}

Note that I have not specified the scope of my FNM feature . This will search, all valid scope, starting with page -> request -> session -> application

If using these values ​​is not $ $, then EL will stop. You can enable it in your Page Directive with such a feature as

  & lt;% @ page language = "java" is ignored = "false"%>  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -