J2EE

复习提纲:

基本知识点:(通过单选、判断、简答来考察)

1.servlet mapping在哪个配置文件中定义?
  • Web.xml

2.servlet 的几个类中,其中一个XXX类含有 getWriter() 方法,返回的是 PrintWriter 类型对象,用于向客户端浏览器输出内容,这个XXX类是什么?
  • ServletResponse

3.HttpServletResponse 类中用来定义 content type 的方法是什么?
  • response.setContentType()

4.处理 servlet life cycle events 的接口或类都有哪些?
  • ServletRequestListener interface
  • ServletRequestAttributeListener interface
  • ServletContextListener interface
  • ServletContextAttributeListener interface
  • HttpSessionListener interface
  • HttpSessionAttributeListener interface
  • HttpSessionActivationListener interface

5.Servlet子类都有哪些?
  • GenericServlet抽象类
  • httpServlet继承了GenericServlet这个抽象类

6.数据库 query string 允许最多有几个字符?
  • 240

7.JSP有哪些隐含可用的对象?
  • application :The application object defines a Web application. Usually, it is the application in the current Web context.
  • config :Represents the object of a ServletConfig class.
  • exception:Represents the Throwable exception, in a JSP page.
  • out:Represents an object of JspWriter to send response to the client. JspWriter extends the PrintWriter class and is used by JSP pages to send client responses.
  • page:Represents the current instance of the JSP page that in turn is used to refer to the current instance of the generated servlet.
  • session:Represents a session object of the HttpSession interface.
  • response:Represents a response object of HttpServletResponse that is used to send an HTML output to the client.
  • request:Represents a request object of HttpServletRequest. It is used to retrieve data submitted along with a request.
  • pageContext:Represents a page context for a JSP page.

8.servlet and CGI 主要区别是什么(和j2ee优缺点比较)?

Advantages and Disadvantages of Java Servlets

Java servlet advantages:

  • Performance (threads are faster than processes)
  • Scalable
  • The Java programming language is robust and object-oriented
  • The Java programming language is platform independent

Java servlet disadvantages:

  • Separation of concerns : business and presentation logic
  • Concurrency issues

Advantages and Disadvantages of CGI programs

CGI program advantages:

  • Written in a variety of languages
  • Relatively easy for a web designer to reference

CGI program disadvantages:

  • Each shell is heavyweights
  • Not scalable
  • CGI processing code (business logic) is mingled with HTML (presentation logic)
  • Language is not always secure or object-oriented
  • Language is not always platform independent

9.WAR 是 J2EE 部署时的打包文件,里面一个什么文件是必须要有的?
  • web.xml

10.HttpSession.getAttribute() 返回什么?
  • Retrieves the object bound with the attribute name specified in the method from the session object. If no object is found for the specified attribute, then the getAttribute() method returns null.

11.DataSource.getConnection() 干什么用的?
  • Attempts to establish a connection with the data source that this DataSource object represents.

12.GenericServlet是做什么的?其中有个 service方法是做什么用的?
  • The GenericServlet class implements the Servlet,ServletConfig and Serializable, is extended by the HttpServlet class, which in turn is extended by a user defined class.
  • The service() method processes the request and returns the response to the Web container.
13.http是什么?有什么特点?
  • The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. It is a generic, stateless protocol.

14.HTTP Post、HTTP Get方法有什么区别?1A.11
  • The data sent using the GET method is appended as a query string to the URL.
  • The POST request sends the data as the HTTP request body.as a result,the data sent does not appear as a part of the URL.

15.Servlet是什么,运行在哪里,生命周期牵扯到哪些方法, 调用顺序, 其中哪些方法是可以覆盖的, 这些方法的参数是什么? Servlet 初始化和销毁方法什么时候会运行?会画生命周期各状态的转换图。
  • Servlet is a server-side program written in java that dynamically extends the functionality of an application server.
  • It runs in the server-side.
  • Method:init(ServletConfig),service(ServletRequest,ServletResponse)[,doPost,doGet,doHead,doPut](HttpServletRequest,HttpServletResponse),destroy,除service外都可覆盖
  • The web container invokes the init() method of the servlet instance during initialization of the servlet.the init() method is invoked only once in the servlet life cycle.
  • The web container calls the destroy () method before removing the servlet instance from the service .the destroy () method is also invoked only once in a servlet life cycle.

16.J2EE 有几种 线程模式?各是什么?SingleThreadModel是什么有何用?
  • Multi-threading model
  • Single-threading model
  • The SingleThreadModel is a interface that helps the web container creates a pool of servlet instances and assigns one instance per request.if the number of requests exceeds the number of instances in the pool ,the requests are queued.

17.web.xml 中可以配置哪些内容?
  • web.xml can control the registration of a servlet, URL mappings, welcome files, Multipurpose Internet Mail Extensions (MIME) types, page level security constraints, and the behavior of a servlet in the distributed environment.

18.ServletContext是做什么用的?都包含哪些方法?怎么取里面的信息?
  • The ServletContext interface provides information to servlets regarding the environment in which they are running. You can use the ServletContext object to find path information of other files of the web application , access other servlets of the web application , and log messages to the application server log file. You can also use the ServletContext object to set attributes that other servlets of your application can access.
  • setAttribute,getAttribute,getAttributeNames,getInitParameter,getMajorVersion,getMinorVersion
  • 已知attribute names,直接通过getAttribute获取相对应的属性值(Object),Attribute未知,先通过getAttributeNames获取所有的属性名,再通过getAttribute迭代得到所有属性值。getInitParameter获取初始化的参数(String)。

19.捕捉 Servlet 生命周期的各种事件 都需要用到 哪些 监听器?
  • Servlet Request listeners
  • Servlet Context listeners
  • Http Session listeners

20.RequestDispatcher做什么用的?2B.3

  • Interface that allows inter-servlet communication.
  • Forward a request to another resource that can be a static HTML or a servlet
  • Include the contents of another resource

21.JSP是什么,怎么运行的? 和Servlet有何关系? 有哪些优缺点?
  • JSP is a technology that helps software developers create dynamically generated web pages based on HTML,XML,or other document types.
  • It uses the Java programming language.To deploy and run JavaServer pages,a compatible web with a servlet container is required
  • 关系:Servelts and jsp share common features,such as platform independence, creation of database-driven web applications,and server side programming capabilities.When the client browser requests for a particular jsp page,the server,in turn,sends a request to the jsp engine.a jsp engine is a web container that compiles a jsp page to a servlet.JSP is translated to a servlet.

Advantages:

  • JSP allows java to be embedded directly into an HTML page by using special tags.the HTML content and java content can also be placed in separate files.any changes made to the html content is automatically compiled and loaded onto the server
  • A jsp page,by virtue of separate placement of the static and dynamic content,facilitates both web developer and web designer to work independently .

Disadvantages:

  • 为了跨平台的功能,为了极度的伸缩能力,所以极大的增加了产品的复杂性(Complexity)。
  • Java的运行速度是用class常驻内存来完成的,所以占内存(Consuming memory)

22.什么是MVC? Web开发中的Boundary, Service, Entity 组件各指什么?什么是DAO?作用是什么?5A.3
  • MVC(model view controller)is a design pattern that seperates data access ,business logic and presentation logic from each.
  • Boundary components have two aspects:views and controllers.
  • Entity components represent real world business objects.
  • Service components provide functional service to the boundary components for manipulating entities.
  • Data Access Object design pattern:
  • Separates object persistence and data access logic from the business logic or persistence mechanism API.
  • Enables you to change the data access code at any point in time without modifying the application code.

23.J2EE编程中的request和response是干什么用的?从 http request stream中能得到哪些信息?http 如何从request中存取信息?存在其中的信息与其他类型如ServletContex中的信息的 scope(

范围)有何区别?

  • Request object is used to represent the request information being sent by an HTTP client,support for retrieving request parameters and accessing HTTP request header information .
  • Response object provides methods to handle response,status codes, and response headers of the servlets that communicate using HTTP.
  • 可以得到信息:HTTP request parameters,HTTP request header information

24.HTTP请求中的请求字符串允许的最大长度是多少?
  • 2083

25.如何从request中拿到http的"Host"值?
  • request.getHeader("Host");
26.如何获取Servlet的初始化参数?
  • getServletConfig().getInitParameter("name");
  • 在web.xml中初始化参数的设定:
<init-param>
    <param-name>name</param-name>
    <param-value>value</param-value>
 </init-param>

27.JSTL是什么?做什么用的?<jsp:useBean> 等相关标签是干什么用的?
  • <jsp:useBean>标签用来在jsp页面中创建一个Bean实例
  • JSTL(JavaServer Pages Standard Tag Library) is a collection of standard tags that allow you to use tags in JSP pages to perform repetitive task,instead of using Java scriptlet code.
  • JSTL is a collection of methods and tags that are common to many JSP applications.You can use JSTL to minimize the Java code in your JSP application.
28.JNDI是什么?DataSource起什么作用,与JNDI有什么关系?JDBC是什么?JNDI与JDBC有什么关系?5A.7
  • 6(Java Naming and Directory Interface) is an API that provides naming and directory services to Java applications.It also provides information about the applications,naming services,networks,and end-users.
    JNDI是一种为Java应用程序提供命名和目录服务的API。它还提供有关应用程序,命名服务,网络和最终用户的信息。
  • DataSource is a resource that contains the information needed to connect to an underlying database.We can use JNDI to bind the DataSource.

29.Filter起什么用作?Filter.init() 是干什么用的?有何特点?FilterChain 是什么类?干什么的?2B.21
  • A servlet filter is an object that intercepts the requests and responses that flow between a client and a servlet.It can modify the headers and contents of a request coming from a web client and forward it to the target servlet,intercept and manipulate the headers and contents of the response that the servlet sends back.
  • Filter.init(): Retrieve the initialization parameters configured in the deployment descriptor.

30.J2EE的会话管理技术有哪些?谁是默认的会话管理方式? Session对象能否在几个Web应用程序间共享?<session-timeout> 干什么用的? 填什么? 意思是什么?
  • cookie(默认),URL Rewriting,Hidden form fields,Servlet session API
  • Session对象可以在几个Web应用程序间共享
  • <session-timeout>.Session-timeout is used to specify the timeout value of the session in minutes
<session-config>
  <session-timeout>30</session-timeout>
</session-config>

31.如何往HttpSession中存取信息?如何得到session对象?2A.15
  • Answer:
HttpSession session=request.getsession(true);`
session.setAttribute("name","vaule");
String value=String(session.getAttribute("name'));

32.Web容器工作过程,会画图示意
1.jpeg

33.J2EE编程里都有哪些scope可以用来存储信息?
  • page、request、session、application
34.J2EE采用的安全机制有哪4个? 之间的区别是什么?
  • Authentication: Identifies a user.
  • Authorization: Specifies the rights assigned to an authenticated user to access resources of a Web application.
  • Data Integrity: Ensures that data is not modified while being transferred between the server and the client.
  • Auditing: Secures Web applications by maintaining a record of the rights assigned to different types of Web users.

35.JavaBeans 是做什么的?
  • A Java Bean is a reusable and a self-contained software component that takes advantage of all the security and platform-independent features of Java. The programmer can create a Java Bean to access the database and develop data processing code in the bean, while the designer can design the user interface in the JSP page. Finally, the Java Bean can be included in the JSP page to start processing user requests.

编程能力:(通过编程题考察)

36.在Servlet里如何得到HTML表单里的参数?
  • request.getParameter("name");
  • HTML page:
<form action="Servlet1" method="post">
  <input type="text" name="name"/>
  <input type="text" name="id"/>
  <input type="submit">
</form>
  • Servlet1 page:
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
{
  String name=request.getParameter("name");
  String id=request.getParameter("id"); 
}

37.从ServletContext中可存取哪些信息, 如何存取?1B.6
  • java对象和初始化信息
  • 存取方法注意父类与子类的转换
    void setAttribute(String,Object)
    Object getAttribute(String)
    String getInitParameter(String)
public class RetrievingCntx extends GenericServlet {
  ServletContext ctx;
  String url;
  Public void init(ServletConfig cfig) {
    ctx = cfig.getServletContext();
  }
  Public void service(ServletRequest request,Servletresponse response)throws ServletException,IOException{
    Ctx.setAttribute("url","abcdefg");   //存
    String url=(String)ctx.getAttribute("url");  //取
  }
}

38.如何通过request来存取及传递信息;2B.7
  • attribute的值类型为Object,注意转换,parameter的值类型为String,ServletContext的initparameter也为String
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
Public class CalculatorServlet extends HttpServlet {
  Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException {
    int num1 = Integer.parseInt(request.getParameter("number1"));
    int num2 = Integer.parseInt(request.getParameter("number2"));
    int result = num1 + num2;
    request.setAttribute("result", new Integer(result));
    ServletContext contx = getServletConfig().getServletContext();
    RequestDispatcher reqDispatcher = contx.getRequestDispatcher("/servlet/DisplayServlet");
    reqDispatcher.forward(request, response);
  } 
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
Public class DispalyServlet extends HttpServlet {
  Public void doGet(HttpServletRequest, HttpServletResponse response) throws ServletException, IOException {
    Integer res = (Integer)request.getAttribute("result");
    PrintWriter pw = response.getWriter();
    Pw.println(res.toString());
  }
}

39.如何实现在 doFilter() 方法中获取客户端的IP地址?
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
  HttpServletRequest request=(HttpServletRequest)req;
  String ip=request.getRemoteAddr();
  chain.doFilter(req, res);
}

40.如何从一个Servlet里通过 RequestDispatcher跳转到另一个Servlet里?2B.5
RequestDispatcher dispatch = getServletConfig().getServletContext().getRequestDispatcher("/HoteInformation");
Dispatch.forward(request, response);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容