A collection of questions and answers about Java technology delivered inside the company

Source: Internet
Author: User
Tags session id

1 Speak the life cycle of the servlet and tell the difference between a servlet and a CGI

When the servlet is instantiated by the server, the container runs its Init method, the service method is run when the request arrives, and the service method automatically dispatches the Doxxx method (Doget,dopost) corresponding to the request, etc. The Destroy method is called when the server decides to destroy the instance.

The difference from CGI is that the servlet is in a server process, it runs its service method in a multithreaded manner, one instance can serve multiple requests, and its instances are generally not destroyed, and CGI generates a new process for each request, which is destroyed after the service is completed. So the efficiency is lower than the servlet.

2 The difference between forward and redirect

Forward is the server internal request resources, the server directly accesses the URL of the destination address, the response content of that URL is read, and then sent to the browser, the browser does not know where the server sent the content from, so its address bar is the original address.

Redirect is the server based on logic, send a status code, tell the browser to request the address again, in general, the browser will use all the parameters just requested again, so the session,request parameters can be obtained.

3 The difference between a dynamic include and a static include in a JSP

A collection of questions and answers about Java technology delivered inside the company

Dynamic include uses Jsp:include action to implement <jsp:include page= "included.jsp" flush= "true"/> It always checks for changes in the included files, is suitable for containing dynamic pages, and can take parameters.

Static include is implemented with the include pseudo-code and does not check for changes in the included files, and is suitable for containing static pages <%@ include file= "included.htm"%>

4 JSP built-in objects and methods

The request represents the HttpServletRequest object. It contains information about the browser request and provides several useful ways to get the cookie, header, and session data.

Response represents the HttpServletResponse object and provides several methods for setting the response to the browser (such as cookies, header information, and so on).

The Out object is an instance of Javax.jsp.JspWriter and provides several methods that you can use to echo the output to the browser.

PageContext represents a Javax.servlet.jsp.PageContext object. It is an API for easy access to a wide range of namespaces, servlet-related objects, and a way to wrap generic servlet-related functionality.

The session represents a requested Javax.servlet.http.HttpSession object. Session can store the user's status information.

Applicaton represents a Javax.servle.ServletContext object. This helps to find information about the servlet engine and the servlet environment.

Config represents a Javax.servlet.ServletConfig object. This object is used to access the initialization parameters of the servlet instance.

Page represents a servlet instance that is generated from this page.

5 Common directives for JSPs

<% @page language= "java" contentype= "text/html;charset=gb2312" session= "true" buffer= "64KB" autoflush= "true" Isthreadsafe= "true" info= "text" errorpage= "error.jsp" iserrorpage= "true" iselignored= "true" pageencoding= "gb2312" Import= "Java.sql.*"%>
Iserrorpage (whether the exception object can be used), iselignored (whether or not to ignore the expression)

<% @include file= "filename"%>
<% @taglib prefix= "C" uri= "/http ..."%>
6 What is the case when calling Doget () and Dopost ()?

The method property in the form tag in the JSP page is called doget () when it is get, and Dopost () is called when it is post.

7 How realistic servlet single-threaded mode

<%@ page isthreadsafe= "false"%>

8 methods for object passing between pages

Request,session,application,cookie, etc.

9 What are the similarities and differences between JSP and servlet, and what is the connection between them?

JSP is the extension of servlet technology, which is essentially a simple way for servlets to emphasize the appearance of application. The JSP is compiled with a "class servlet". The main difference between Servlets and JSPs is that the application logic of the servlet is in the Java file and is completely detached from the HTML in the presentation layer. The case for JSP is that Java and HTML can be combined into a file with a. jsp extension. JSPs focus on Views, and Servlets are used primarily for control logic.

104 Types of Session tracking technology

Page is the object and property that represents a page. A page is represented by a compiled Java servlet class (which can have any include directives, but no include actions). This includes both Servlets and JSP pages that are compiled into Servlets.

Request is the object and property that is related to a request made by a WEB client. A request may span multiple pages, involving multiple WEB components (due to the relationship between the forward directive and the include action).

A session is a representation of the objects and properties associated with a user experience for a WEB client. A Web session can and will often span multiple client requests.

Application is a representation of objects and properties related to the entire WEB application. This is essentially a global scope that spans the entire WEB application, including multiple pages, requests, and sessions.

The main method of the Request object

SetAttribute (String name,object): Sets the parameter value of the request named name

GetAttribute (String name): Returns the property value specified by name

Getattributenames (): Returns a collection of names of all properties of the request object, resulting in an instance of an enumeration

GetCookies (): Returns all cookie objects for the client, as a result of a cookie array

Getcharacterencoding (): Returns the character encoding in the request

Getcontentlength (): Returns the length of the requested body

GetHeader (String name): Gets the file header information defined by the HTTP protocol

Getheaders (String name): Returns all values of the request header for the specified name, resulting in an instance of an enumeration

Getheadernames (): Returns the name of the request header, and the result is an instance of the enumeration

getInputStream (): Returns the requested input stream for obtaining the data in the request

GetMethod (): Obtaining a client's way of transmitting data to the server side

GetParameter (String name): Gets the parameter value specified by the client that is passed to the server side by name

Getparameternames (): Gets the name of all parameters that the client sends to the server side, and the result is an instance of the enumeration

Getparametervalues (String name): Gets all values that have the parameter specified by name

Getprotocol (): Gets the protocol name on which the client transmits data to the server

GetQueryString (): Get query string

Getrequesturi (): Gets the client address that issued the request string

GETREMOTEADDR (): Gets the IP address of the client

Getremotehost (): Gets the name of the client

GetSession ([Boolean create]): Returns and requests the relevant session

getServerName (): Gets the name of the server

Getservletpath (): Gets the path of the script file requested by the client

Getserverport (): Gets the port number of the server

RemoveAttribute (String name): Delete One of the properties in the request

Is it a technology or a platform or a framework?

The Java EE itself is a standard, a standard platform for the development of enterprise distributed applications. The Java EE is also a framework, including JDBC, JNDI, RMI, JMS, EJB, JTA and other technologies.

The Java EE is a completely different technology architecture than traditional application development, including many components, which can simplify and standardize the development and deployment of application system, thus improve the portability, security and reuse value, the core of which is a set of technical specifications and guidelines, including the various components, service architecture and technical level, have common standards and specifications, so that the different platforms in accordance with the Java EE architecture, there is a good compatibility between the past, the use of information products used in enterprise back-end can not be compatible with each other, resulting in internal or external difficult to exchange the dilemma.

What are the general implementations of the servlet implementation method?

public void init (servletconfig config) public servletconfig getservletconfig () public String getservletinfo () public void Service (ServletRequest request,servletresponse response) public void Destroy ()
14 please explain (or simply describe) the following nouns commonly used in the Java EE

Web container: Provides an environment for the application components (Jsp,servlet) in which the Jsp,servlet directly and the environment variables in the container are connected to XXX, without concern for other system problems. There are mainly Web servers to implement. For example: Tomcat,weblogic,websphere and so on. The interface provided by this container strictly complies with the Web application standard in the Java EE specification. Our web server, which adheres to the above criteria, is called the Web container in Java EE.

EJB container: Enterprise java bean container. It has the industry domain characteristic more. He provides various management functions for the EJB component that runs in it. As long as the EJB that satisfies the Java EE specification is placed in the container, it is immediately managed efficiently by the container. and the system-level services can be obtained through the existing interfaces. For example, mail service, transaction management.

JNDI: (Java Naming & directory Interface) Java named directory service. The main functionality is to provide a directory system that allows applications from other parts of the world to leave their index on top of them, thus satisfying the ability to quickly find and locate distributed applications.

JMS: (Java message Service) Java Messaging Services. The main implementation of communication between the various applications. Includes point-to-point and broadcast.

JTA: Java Transaction Service (Java Transaction API). Provides a variety of distributed transaction services. The application simply invokes the interface it provides.

JAF: Java Security Authentication Framework (Java Action Framework). Provide some framework for security control. Let developers implement their own personality security control strategies through a variety of deployments and customizations.

RMI/IOP: (Remote Method invocation/internet Object Request Mediation Protocol) they are primarily used to invoke services remotely. For example, a program that runs on a remote computer provides stock Analysis Services that we can invoke directly on the local computer. Of course, it is necessary to pass a certain specification in order to communicate between heterogeneous systems. RMI is unique to Java.

What are the technologies that all of the various parts of MVC have to achieve?

MVC is shorthand for Model-view-controller. "Model" represents the business logic of the application (implemented through the JAVABEAN,EJB component), "View" is the presentation face of the application (generated by the JSP page), and "Controller" is the process control that provides the application (typically a servlet), Through this design model, the application logic, processing and display logic are divided into different component implementations. These components can be interacted with and reused.

What are the actions of the JSP? What are the roles?

JSP has the following 6 basic actions:

Jsp:include: A file is introduced when the page is requested.
Jsp:usebean: Find or instantiate a JavaBean.
Jsp:setproperty: Sets the properties of the JavaBean.
Jsp:getproperty: Outputs the properties of a JavaBean.
Jsp:forward: Move the request to a new page.
Jsp:plugin: Generates an object or embed tag for the Java plug-in based on the browser type.
The difference and connection between the cookie and session

The cookie mechanism uses a scheme that maintains state on the client, while the session mechanism uses a scheme that maintains state on the server side.

Cookie mechanism: Orthodox cookie distribution is implemented by extending the HTTP protocol, and the server prompts the browser to generate the appropriate cookie by adding a special line of instructions to the HTTP response header. However, purely client-side scripts such as JavaScript or VBScript can also generate cookies. And the use of cookies by the browser in accordance with certain principles in the background automatically sent to the server. The browser checks all stored cookies and, if a cookie declares a scope greater than or equal to the location of the resource to be requested, sends the cookie to the server on the HTTP request header of the requesting resource.

The contents of the cookie mainly include: name, value, expiration time, path and domain. The path together with the domain constitutes the scope of the cookie. If you do not set an expiration time, the cookie will be closed for the duration of the browser session. This cookie, which is the lifetime of the browser session, is referred to as a session cookie.

Session cookies are generally not stored on the hard disk but are kept in memory, although this behavior is not regulated. If the expiration time is set, the browser will save the cookie to the hard disk, turn it off and open the browser again, and the cookies remain valid until the set expiration time expires. Cookies stored on the hard disk can be shared between different browser processes, such as two IE windows. For cookies stored in memory, different browsers have different ways of handling them.

Session mechanism: Session mechanism is a server-side mechanism, the server uses a hash-like structure (or perhaps a hash table) to save information.

With 1-5 working experience, in the face of the current popular technology do not know where to start, need to break through the technical bottleneck can add group. Stay in the company for a long time, have a very comfortable, but job-hopping interview wall. Need to study in a short period of time, job-hopping to get a high salary can add group. If there is no work experience, but the foundation is very solid, on the Java work mechanism, common design ideas, Common Java Development Framework Master skilled can add group. Java Architecture Group: 5,912,408,171 communication.

When a program needs to create a session for a client's request, the server first checks to see if the client's request contains a session ID (called the session ID.), and if it is included, it has previously created a session for this client. The server will follow the session ID to retrieve the session (not retrieved, a new one), if the client request does not include session ID, then create a session for this client and generate a session ID associated with this session, The value of session ID should be a string that is neither duplicated nor easy to be found, and the session ID will be returned to the client in this response to save. This session ID can be saved by using a cookie so that the browser can automatically send the tag to the server in the interactive process. Generally the name of this cookie is similar to Seeesionid. However, a cookie can be artificially banned, and there must be other mechanisms that can still pass the session ID back to the server when the cookie is banned.

A technique that is often used is called URL rewriting, which attaches the session ID directly behind the URL path.

There is also a technique called form-hidden fields. Is that the server automatically modifies the form, adding a hidden field so that the session ID can be passed back to the server when the form is submitted. Like what:

<form name= "Testform" action= "/xxx" > <input type= "hidden" name= "Jsessionid" value= " byok3vjfd75apnrf7c2hmdnv6qzcebzwowibyenlerjq99zwpbng!-145788764 "> <input type=" text "> </form>
In fact, this technique can be replaced simply by applying URL rewriting to the action.

The difference between a Cookie and a session:

The cookie data is stored on the client's browser and the session data is placed on the server.
Cookies are not very secure, and others can analyze cookies stored locally and make cookie spoofing, taking into account that security should use the session.
The session will be saved on the server for a certain amount of time. When the number of accesses increases, the performance of your server will be compared, and cookies should be used in consideration of mitigating server performance.
The limit for a single cookie on the client is 3K, which means that the cookie stored by a site at the client cannot be 3 K.
The built-in objects and functions of JSP

The JSP has the following 9 basic built-in components (which correspond to the 6 internal components of ASP):

Request client requests, this request will contain parameters from the Get/post request
Response Web page back to the client's response
The properties of the PageContext Web page are managed here
Session period associated with the request
What the application servlet is doing
Output used to transmit a response
Config Servlet's architectural parts
Page JSP webpage itself
Exception exceptions for error pages, not captured
What is the difference between the two ways of jumping in JSP?

Jump with request parameter
Request.setattribute ("param", "value"); Request.getrequestdispatcher ("test.jsp"). Forward (request, response);
Parameters can be obtained on the destination page:
String Value = Request.getattribute ("param") ==null?:( String) request.getattribute ("param");
Jumps without the request parameter
Response.sendredirect ("test.jsp");
The difference between forward and redirect

Forward is the server internal redirect, the program receives the request to redirect to another program, the client does not know;

Redirect is the server received a request after sending a state header to the customer, the customer will request again, here more than two network traffic. Of course, forward also has shortcomings, that is, the path of the forward page if the relative path will have some problems.

Forward will bring the request state, Bean, and so on to a JSP. Redirect is sent to client side once again request, so the data is not retained.
Using forward, you can use GetAttribute () to fetch the bean and other data from the previous JSP.

From the Address bar display
Forward is the server requests the resource, the server directly accesses the URL of the destination address, reads the response content of that URL, and then sends the content to the browser. The browser does not know where the server sent the content from, so its address bar is still the original address.
Redirect is the server based on logic, sending a status code that tells the browser to re-request that address. So the address bar shows a new URL. So redirect is equal to the client issuing two requests and receiving two response.
From data sharing,
Forward: The forwarded page and the forwarded page can share the data within the request. Redirect: Data cannot be shared. Redirect not only can redirect to other resources of the current application, but can also redirect resources to other applications on the same site, even to other sites using absolute URLs. Forward, a method can only forward requests between resources within the same Web application. Forward is an operation inside the server. Redirect is the server notification client, which lets the client re-initiate the request. So, you can say that redirect is an indirect request, but you can't say "a request belongs to forward or redirect".
From the place of use,
Forward: Generally used for user login, according to the role of forwarding to the corresponding module. Redirect: Generally used for users to log off when landing to return to the main page and jump to other sites and so on.
From an efficiency standpoint
Forward: High. Redirect: Low.
21 describe the differences between JSP and servlet, the common denominator, the scope of their application

JSPs are essentially servlets, but they are created differently. Servlet is completely Java program code composition is good at process control and transaction processing and through the servlet to generate dynamic Web pages; JSP is composed of HTML code and JSP tags, it is convenient to write dynamic Web pages, so in practical application, servlet is used to control business process, and JSP is used to generate dynamic Web pages. In the struts framework, the JSP is in the view layer of the MVC design pattern, And the servlet is in the control layer.

22 Lists the 2 ways in which the JSP contains external files, and how they differ from each other.

<jsp:include page= "b.jsp"/>
<%@ include file= "b.jsp"/>
Difference:

<jsp:include page= "b.jsp"/> (first executed, then included)
This label notation: dynamically differentiate whether a dynamic or static page is added to a static page to include (only its text) the resource directly.
<%@ include file= "b.jsp" > this directive means: statically contains the page, regardless of its content, but static or dynamic pages are first added to the contents of the page.
<jsp:include page= "b.jsp"/> can pass parameters, as follows:
<jsp:include page= "b.jsp" > <jsp:param name= "parameter name" value= "argument value"/> </jsp:include>
How to implement request forwarding in both JSP and servlet respectively

There are two ways to forward a servlet:

Response.sendredirect (Response.encodeurl (relative path or absolute path)); Request.getrequestdispatcher (relative path). Forward ();
JSP is a servlet extension that can be used in addition to the above two types of tags:

<jsp:forward page= "relative path"/>

A collection of questions and answers about Java technology delivered inside the company

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.