简体   繁体   English

JSP和Java Servlet问题

[英]JSP and Java Servlet problems

I am new to JSP and Java Servlet. 我是JSP和Java Servlet的新手。 I am quite confusing about Session object. 我对Session对象感到困惑。 I saw session When I learned PHP session and cookie. 当我学习了PHP会话和cookie时,看到了会话。 Are there complete different things? 有完全不同的东西吗? And how a Session object is created, structured and used. 以及如何创建,构造和使用Session对象。 This object is in JSP or Java Servlet? 这个对象是在JSP还是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. 另外,在什么情况下适合使用JSP页面?(何时应使用Java Servlet,何时应使用Java Servlet Page)。例如,对于Java Servlet对象,运行了一个电子邮件站点。 There will be a lot of users. 会有很多用户。 How does one Java Servlet object deal with interactions from so many browsers?(like hundreds of logging, reading, etc.)I know there should be only one copy of Java Servlet object exists. 一个Java Servlet对象如何处理来自这么多浏览器的交互?(例如成百上千的日志记录,阅读等)我知道应该只有一个Java Servlet对象副本。 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. 以及如何创建,构造和使用Session对象。

It depends on the implementation of it, here is the contract 取决于执行情况, 是合同

This object is in JSP or Java Servlet? 这个对象是在JSP还是Java Servlet中?

This is as an implicit object in jsp and it can be retrieved from request instance from servlet's service method 这是jsp中的隐式对象,可以从servlet的service方法的request实例中检索它。

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. JSP页面适合什么情况?(何时应使用Java Servlet,何时应使用Java Servlet页面)。例如,对于Java Servlet对象,运行了一个电子邮件站点。 There will be a lot of users. 会有很多用户。 How does one Java Servlet object deal with interactions from so many browsers 一个Java Servlet对象如何处理来自众多浏览器的交互

Use jsp as view servlet as controller, See MVC 将jsp用作视图servlet作为控制器,请参见MVC

now there should be only one copy of Java Servlet object exists. 现在应该只存在一个Java Servlet对象的副本。 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. 每个请求都在不同的线程中服务,因此为什么要创建不同的实例,我们可以让servlet的一个实例为我们完成所有这些工作。 and its alive until garbage collection clears it 直到垃圾回收清除它为止

See : Head First 请参阅:先行

我认为,如果您查看Java Servlet生命周期,将会回答很多问题。

  1. 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 每个用户都有一个ID为jsessionid的会话,该会话的结构通常是一个存储键值的映射数据结构

in Servlert you can construct a session object like this 在Servlert中,您可以像这样构造一个会话对象

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. 会话对象存在于servlet和jsp中,而btw jsp最终是一个servlet,但不同之处在于,当您想在jsp页面中使用会话时,无需构造它。 its defeind by default just use it 默认情况下取消默认使用

session.setAttribute(string,object);
  1. 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 当页面包含许多html元素并且具有很多设计和jsp时使用的JSP页面,另一方面,您可以将servlet用作jsp页面,但是可以处理html源代码的每一行。

JSP is preferred as a view in the MVC model and the servlet as controller . 在MVC模型中,首选JSP作为视图,而将Servlet作为控制器。

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 . 服务器为每个servlet保留一个对象,当一个新请求到来时,servlet对象将新请求(客户端)放在一个新的线程中,因此如果您一次有100个客户端,服务器中有100个线程。 but you can configure the server to construct more than one object of a servlet . 但是您可以配置服务器以构造servlet的多个对象。

i hope i could help u .. 我希望我能帮助你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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