Home Java JavaInterview questions 2020 New Java Interview Questions - Java Web (2)

2020 New Java Interview Questions - Java Web (2)

Jun 16, 2020 pm 05:29 PM
java java web Interview questions

2020 New Java Interview Questions - Java Web (2)

#1. If the client prohibits cookies, can the session still be used?

Cookie and Session are generally considered to be two independent things. Session uses a solution that maintains state on the server side, while Cookie uses a solution that maintains state on the client side. But why can't I get the Session if I disable cookies? Because the Session uses the Session ID to determine the server Session corresponding to the current conversation, and the Session ID is passed through Cookie, disabling Cookie is equivalent to losing the Session ID, and thus the Session is lost.

Assuming that the user uses Session when turning off Cookie, the implementation methods are as follows:

  • Set "session.use_trans_sid = 1 in the php.ini configuration file ", or turn on the "--enable-trans-sid" option when compiling to let PHP automatically pass the Session ID across pages.

  • Manually pass the value through the URL and pass the Session ID through the hidden form.

  • Save the Session ID in a file, database, etc., and call it manually during the cross-page process.

2. What is the difference between spring mvc and struts?

1. Differences in interception mechanisms

Struts2 is a class-level interception. Each request will create an Action. When integrating with Spring, the ActionBean injection scope of Struts2 is prototype mode. prototype, and then inject the request data into the property through setter and getter. In Struts2, an Action corresponds to a request and response context. When receiving parameters, it can be received through attributes. This shows that attribute parameters are shared by multiple methods. A method of Action in Struts2 can correspond to a URL, but its class attributes are shared by all methods. This means that it is impossible to use annotations or other methods to identify its method, and it can only be designed as multiple instances.

SpringMVC is a method-level interception. One method corresponds to a Request context, so the method is basically independent and has exclusive access to request and response data. Each method corresponds to a URL at the same time. The parameter passing is directly injected into the method, which is unique to the method. The processing results are returned to the framework through ModeMap. During Spring integration, SpringMVC's Controller Bean defaults to singleton mode, so by default, only one Controller will be created for all requests. There should be no shared properties, so it is thread-safe. If you want to change the default scope, Need to add @Scope annotation modification.

Struts2 has its own interceptor mechanism. SpringMVC uses an independent Aop method, which causes the amount of configuration files of Struts2 to be larger than that of SpringMVC.

(Related tutorials recommended: java entry program)

2. Differences in underlying frameworks

Struts2 is implemented using Filter (StrutsPrepareAndExecuteFilter), SpringMVC (DispatcherServlet ) is implemented using Servlet. Filter is initialized after the container is started; it crashes after the service is stopped, later than Servlet. Servlet is initialized when called, before Filter is called, and is destroyed after the service stops.

3. In terms of performance

Struts2 is a class-level interception. Each request corresponds to a new Action of the instance, and all attribute value injection needs to be loaded. SpringMVC implements zero configuration. Since SpringMVC is based on Method interception involves loading a singleton mode bean for injection. Therefore, SpringMVC development efficiency and performance are higher than Struts2.

4. In terms of configuration,

spring MVC and Spring are seamless. The management and security of this project are also higher than Struts2.

3. How to avoid sql injection?

  • PreparedStatement (simple and effective method)

  • Use regular expressions to filter incoming parameters

  • String filtering

  • Call this function in JSP to check whether it contains illegal characters

  • JSP page judgment code

4. What is an XSS attack and how to avoid it?

XSS attack is also called CSS, and the full name is Cross Site Script (cross-site scripting attack). The principle is that the attacker enters malicious HTML code into a website with XSS vulnerabilities. When the user browses the website , this HTML code will be automatically executed to achieve the purpose of attack.

XSS attacks are similar to SQL injection attacks. In SQL injection attacks, SQL statements are used as user input to query/modify/delete data. In XSS attacks, malicious scripts are inserted to target users. Browser control to obtain some user information. XSS is a common vulnerability in Web programs. XSS is a passive attack method used on the client side.

The general idea of ​​​​XSS prevention is: filter the input (and URL parameters) and encode the output.

(Video tutorial recommendation: java video tutorial)

5. What is a CSRF attack and how to avoid it?

CSRF (Cross-site request forgery) is also called one-click attack or session riding. The full Chinese name is cross-site request forgery. Generally speaking, an attacker forges a request from the user's browser and sends it to a website that the user has authenticated to visit, so that the target website receives and mistakenly thinks it is the user's real operation and executes the command. Commonly used to steal accounts, transfer money, send false messages, etc. The attacker exploits the website's request verification vulnerability to implement such an attack. The website can confirm that the request originates from the user's browser, but cannot verify whether the request originates from the user's true intention.

How to avoid:

1. Verify the HTTP Referer field

The Referer field in the HTTP header records the source address of the HTTP request. Under normal circumstances, the request to access a security-restricted page comes from the same website, and if a hacker wants to implement a CSRF attack on it, he can generally only construct the request on his own website. Therefore, CSRF attacks can be defended by verifying the Referer value.

2. Use the verification code

Add the verification code to the key operation page. After receiving the request, the background can judge the verification code to prevent CSRF. But this method is not very user friendly.

3. Add token to the request address and verify

The reason why the CSRF attack is successful is that the hacker can completely forge the user's request, and all user verification information in the request exists in cookies, so hackers can directly use the user's own cookies to pass security verification without knowing the verification information.

To resist CSRF, the key is to put information in the request that hackers cannot forge, and this information does not exist in cookies. You can add a randomly generated token as a parameter to the HTTP request, and create an interceptor on the server side to verify the token. If there is no token in the request or the token content is incorrect, it is considered that it may be a CSRF attack and the request will be rejected. .

This method is safer than checking the Referer. The token can be generated after the user logs in and placed in the session. Then the token can be taken out of the session at each request and matched with the token in the request. Compare, but the difficulty of this method is how to add the token to the request in the form of parameters.

For GET requests, the token will be appended to the request address, so that the URL becomes

http://url?csrftoken=tokenvalue
Copy after login

. For POST requests,

<input type="hidden" name="csrftoken" value="tokenvalue"/>
Copy after login
## must be added at the end of the form. #In this way, the token is added to the request in the form of a parameter.


4. Customize attributes in the HTTP header and verify

This method also uses tokens for verification. The difference from the previous method is that the token is not used here. Instead of placing it in the HTTP request as a parameter, put it in a custom attribute in the HTTP header. Through the XMLHttpRequest class, you can add the csrftoken HTTP header attribute to all requests of this type at once, and put the token value into it.

This solves the inconvenience of adding token to the request in the previous method. At the same time, the address requested through XMLHttpRequest will not be recorded in the browser's address bar, and there is no need to worry about the token being leaked to other websites through the Referer. Go in.

If you want to know more related interview questions, you can visit the

java interview questions column.

The above is the detailed content of 2020 New Java Interview Questions - Java Web (2). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles