JSP and Java Servlet problems
I am new to JSP and Java Servlet. I am quite confusing about Session object. I saw session When I learned PHP session and cookie. Are there complete different things? And how a Session object is created, structured and used. This object is in JSP or Java Servlet? could somebody tell me this by words(like concept). In addition, in what situation a JSP page would be appropriate for?(when should I use a Java Servlet and when should I use a Java Servlet Page).For Java Servlet object, for example, ran an email site. There will be a lot of users. How does one Java Servlet object deal with interactions 开发者_JS百科from so many browsers?(like hundreds of logging, reading, etc.)I know there should be only one copy of Java Servlet object exists. But why? If only one there, when is it created and destroyed. Ah... So many questions. If someone can help me I will really grateful for this. Thanks a million!
? And how a Session object is created, structured and used.
It depends on the implementation of it, here is the contract
This object is in JSP or Java Servlet?
This is as an implicit object in jsp and it can be retrieved from request
instance from servlet's service method
what situation a JSP page would be appropriate for?(when should I use a Java Servlet and when should I use a Java Servlet Page).For Java Servlet object, for example, ran an email site. There will be a lot of users. How does one Java Servlet object deal with interactions from so many browsers
Use jsp as view servlet as controller, See MVC
now there should be only one copy of Java Servlet object exists. But why? If only one there, when is it created and destroyed.
each request is served in different thread so why to create different instance, we can have one instance of servlet doing all this for us. and its alive until garbage collection clears it
See : Head First
- You can think about a session object as a file . every user have a session with id called jsessionid , the structure of a session is normally a map data structure which store key value
in Servlert you can construct a session object like this
HttpSession session = request.getSession(true);
then you can add item to the session like this
session.setAttribute(string ,object); ex : session.setAttribute("username","foo");
the session object exist in servlet and jsp , and btw jsp eventually is a servlet but the difference is that when u want to use session in a jsp page there is no need to construct it. its defeind by default just use it
session.setAttribute(string,object);
- JSP page used when a page contains a lot of html element and have a lot of design and jsp let you maintain the page easily on the other hand you can use servlet as jsp page but you will deal with every line o html source code
JSP is preferred as a view in the MVC model and the servlet as controller .
the server keeps one object for each servlet and when a new request come the servlet object put the new request (client ) in a new thread so if you have 100 client at a time the is 100 thread in server . but you can configure the server to construct more than one object of a servlet .
i hope i could help u ..
I think many of your questions would be answered if you had a look at the Java Servlet Life Cycle.
精彩评论