[] JavaWeb part interview preparation

JavaWeb part

Caused his interview preparation questions, add your own and integrate other people sharing!

1. Tomcat optimization experience

Remove monitoring of web.xml and the editor jsp advance into Servlet.
A surplus of physical memory, the increased use of tomcat jvm memory

2. Explain what is servlet

The controller is equivalent to a processing request.

3. talk about Servlet life cycle?

Servlet life cycle is divided into three stages:
  an initialization stage calls init () method
  2, phase response to client requests call service () method
  3, call termination phase destroy () method

4. Servlet when it was created

1, by default, when the first WEB client requests access to a Servlet's, WEB Servlet container creates an instance of this.
2, when the web.xml file if the elements specified in sub-elements, Servlet container starts when the web server, the Servlet object is created and initialized in the order.

5. Servlet works

1. The client sends a request
2.Servlet call-Service ()
3. calls the doGet, the doPost ()
4. calls business layer
5. The generated response to the client

6. SERVLET API in the forward () and redirect () the difference?

forward: the address bar does not change, the request once completed on the server.
redirect: changing the address bar, 1 or 2 times a request, the client is finished.

Call doGet () and doPost () 7. Under what circumstances?

Jsp page FORM tag attributes of method calls doGet is get (), doPost call is post ().

The main object method 8.Request

setAttribute(String name,Object):设置名字为 name 的 request 的参数值

getAttribute(String name):返回由 name 指定的属性值

getAttributeNames():返回 request 对象所有属性的名字集合,结果是一个枚举的实例

getCookies():返回客户端的所有 Cookie 对象,结果是一个 Cookie 数组

getCharacterEncoding():返回请求中的字符编码方式

getContentLength():返回请求的 Body 的长度

getHeader(String name):获得 HTTP 协议定义的文件头信息

getHeaders(String name):返回指定名字的 request Header 的所有值,结果是一个枚举的

实例

getHeaderNames():返回所以 request Header 的名字,结果是一个枚举的实例

getInputStream():返回请求的输入流,用于获得请求中的数据

getMethod():获得客户端向服务器端传送数据的方法

getParameter(String name):获得客户端传送给服务器端的有 name 指定的参数值

getParameterNames():获得客户端传送给服务器端的所有参数的名字,结果是一个枚举的

实例

getParametervalues(String name):获得有 name 指定的参数的所有值

getProtocol():获取客户端向服务器端传送数据所依据的协议名称

getQueryString():获得查询字符串

getRequestURI():获取发出请求字符串的客户端地址

getRemoteAddr():获取客户端的 IP 地址

getRemoteHost():获取客户端的名字

getSession([Boolean create]):返回和请求相关 Session

getServerName():获取服务器的名字

getServletPath():获取客户端所请求的脚本文件的路径

getServerPort():获取服务器的端口号

removeAttribute(String name):删除请求中的一个属性

9. JSP common instruction

isErrorPage (if you can use the Exception object), isELIgnored (whether or not ignore expressions)

What is the difference 10. request.getAttribute () and request.getParameter ()?

getParameter get is of type String. Or HTTP: //a.jsp the above mentioned id = 123? 123 in, or past data submitted by a form. getAttribute it can be an object.

11. jsp which have built-in objects? What role are? Is there any way were?

JSP 共有以下9个内置的对象:
Page: JSP 网页本身,表示从该页面产生的一个 servlet 实例
Exception: 针对错误网页,未捕捉的例外
Request: 表示 HttpServletRequest 对象。它包含了有关浏览器请求的信息,并且提供了几个用于获取 cookie, header,和 session 数据的有用的方法。
Response: 表示 HttpServletResponse 对象,并提供了几个用于设置送回浏览器的响应的方法(如 cookies,头信息等)
Out: 对象是 javax.jsp.JspWriter 的一个实例,并提供了几个方法使你能用于向浏览器回送输出结果。
pageContext: 表示一个 javax.servlet.jsp.PageContext 对象。它是用于方便存取各种范围的名字空间、servlet 相关的对象的 API,并且包装了通用的 servlet 相关功能的方法。
Session: 表示一个请求的 javax.servlet.http.HttpSession 对象。Session 可以存贮用户的
Applicaton: 表示一个 javax.servle.ServletContext 对象。这有助于查找有关 servlet 引擎和 servlet 环境的信息
Config: 表示一个 javax.servlet.ServletConfig 对象。该对象用于存取 servlet 实例的初始化参数。

12. jsp What action have? What role are?

JSP 共有以下6种基本动作

jsp:include:在页面被请求的时候引入一个文件。

jsp:useBean:寻找或者实例化一个 JavaBean。

jsp:setProperty:设置 JavaBean 的属性。

jsp:getProperty:输出某个 JavaBean 的属性。

jsp:forward:把请求转到一个新的页面。

jsp:plugin:根据浏览器类型为 Java 插件生成 OBJECT 或 EMBED 标记

13. The method of page object passed between

request,session,application,cookie 等

14. What are the two kinds of ways to jump? What is the difference?

There are two types, namely:

<jsp:include page=included.jsp flush=true>

<jsp:forward page= nextpage.jsp/>

The former page does not include turning pages referred to, simply display the results page, the main page or the original page. After the implementation will come back, the equivalent of function calls. And you can take parameters. The latter completely turned a new page and never return.

15.JSP and Servlet What are the similarities and differences What is the connection between them is?

JSP is an extension of the Servlet technology, Servlet is a simple way of essence, more emphasis on the appearance of the application of the expression. JSP
compiled a "class servlet". The advantages of JSP is good at creating web pages, generate dynamic pages more intuitive, the disadvantage is not easy to trace and debug.
Servlet is a pure Java language, processes and specializes in business logic, the disadvantage is not intuitive generate dynamic web pages.

Servlet and JSP main difference that the application logic is a Java Servlet file, and completely separated from the HTML presentation layer. The situation JSP is Java and HTML can be combined into a .jsp file extension. JSP focuses on view, Servlet mainly for control logic.

16. The various parts of the MVC have those technologies to achieve? How to achieve?

MVC is the Model-View-Controller shorthand. Model represents the business logic of the application (by JavaBean, EJB component implementation), View application is a surface (generated by JSP pages), Controller provides process control application (typically a Servlet), such a design model the application logic, processing and display logic implemented into different components. These components can interact and reuse.

17. We often encounter some kind of output encoded characters, such as iso8859-1 and other web application development process, how a certain output encoded string?

Public String translate(String str) {
String tempStr ="";
try {
  tempStr=newString(str.getBytes("ISO-8859-1"), "GBK");
  tempStr=tempStr.trim();
}
catch (Exception e) { 
  System.err.println(e.getMessage());
}
return tempStr;
}

18. servlet package is the jar? Write servlet class needs to inherit what?

servlet-api.jar
HttpServlet

19. Write a method servlet usually be achieved?

doGet, doPost, init, destroy方法

What 20. doGet method doPost method and the two parameters?

httpServletRequest
httpServletResponse

21. How to obtain the parameters

request.getParameter- obtain a single value
request.getParameterValues- obtain multi-valued

21. forward, sendRedirect corresponding method in a Servlet?

RequestDispatcher rd = request.getRequestDispatcher ( "target file");
rd.forward (Request, the Response);
response.sendRedirect ( "target file");

22.servlet be configured in which file?

web.xml-web deployment descriptor file. WEB-INF \ web.xml

23. talk about the role of servlet filter?

servlet filter is a special servlet, can filter specific request path, before accessing this path, execute the filter, the filter is pretreated, before subsequent processing.

24. talk about the role of servlet listener?

servlet listener listens for specific events, when these events occur, the listener will execute the code.

25. servlet API of two major packages?

javax.servlet.;
javax.servlet.http.
;

26. give the user response information, how to do?

response.setContentType(“text/html, charset=“GBK””);
PrintWriter out = response.getWriter();
out.println("…");

27. compiled servlet class on what position?

On the web application WEB-INF folder in the following classes.

28. What is the web container?

J2EE web application specification to achieve agreement. The protocol defines the runtime environment of web applications, including: concurrency, security, lifecycle management, and so on.

29. jsp in execution?

1) The client sends REQUEST request
2) jsp servlet container jsp translates source code
3) the resulting source code after the servlet compiled, loaded in the memory
4) in response to the result of response to the client

30.4 kinds of scopes

page: a page on behalf of related objects and attributes. Static can be shared include the objects and attributes
request: a request on behalf of related objects and attributes.
session: representatives of the objects and attributes associated with a user.
application: on behalf of the entire web application associated with the objects and attributes.

31. jsp four kinds of filters

request,forward,include,error

32. jsp executed in the browser cache

response.setHeader(“Expires”, 0);
response.setHeader(“Cache-Control”, “no-cache”);
response.setHeader(“Pragma”, “no-cache”);

33. jsp cookie set

Cookie c = new Cookie(“name”, “tomzhang”);
response.addCookie©;

34. jsp delete the cookie

c.setMaxAge(0);

35. jsp jsp how to stop execution of

<%
IF (conditional expression) {
// do something
} the else {
return;
}
%>

36. When a result set, how to prevent the field is "null" displayed in html text field?

<%!
String blanknull(String s){
if(s == null or s.equals("")){ s = "";}
return s;
}
%>
<input type ="text" name="name" value="<%=blanknull(name)%>"/>

37. How to get the current number of session?

Use HttpSessionBindingListener listener to track.

38. getSession (true) and getSession (false) the difference?

request.getSession (true): If the session does not exist to create a new session (If you want to write data into the session)
request.getSession (false): if the session does not exist return null (If you just want to read session The data)

Guess you like

Origin blog.csdn.net/weixin_43266090/article/details/90301828