The value and contrast of JSP and servlet

Source: Internet
Author: User

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.

This article discusses the following four aspects of the JSP and servlet transfer value problem:

first, the JSP value to the servlet

There are several forms of JSP value to the servlet: Form table only son value, url value, other ways to pass the value, the following use by default El expression.

The taocan.do,login.do in use needs to be configured in Web. XML with the following code

<servlet>
<servlet-name>taocan</servlet-name>
<servlet-class> (corresponding package name + class name) taocanservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>taocan</servlet-name>
<url-pattern>/taocan.do</url-pattern>
</servlet-mapping>

A, form table only son value:

JSP pages are:
<form action= "Login.do?number=${number}" method= "POST" >
<input type= "text" placeholder= "username" name= "username" >
<input type= "password" placeholder= "password" name= "password" >
<input type= "Submit" value= "Submit" >
</form>
Sevlet, in the program as follows:
String username = request.getparameter ("username");
String Password = request.getparameter ("password");
Can get username,password of JSP transmission
b, URL transfer value

For example, here the <a> tag of the href= "Login.do?number=${number}" with the <form> tag action= "Login.do?number=${number}", in
Servlets are also obtained using Request.getparameter ("number");

C, Java code transfer value

Java fragment code, the servlet can only receive the contents of the Session.setattribute ("Testsession", "Hello Session"), and cannot receive the inside of the request
Capacity. Use Request.getsession (). getattribute ("Testsession") in the servlet to get the session content.

But actually in the page section generally does not write the Java fragment code directly, but in the Javascipt code to pass.

Some of the forms passed in Javascipt are as follows:

1, direct jump, in the page call the following method to implement the jump.
var username= "${user.username}";
function SUBM () {
window.location.href= "Login.do?username=" +username;
}

2. Page definition ID and Method dynamic transfer value:

jquery syntax is used here, please import the appropriate package before use (for example: Jquery-3.1.1.js)

<TD id= "${cart.cartid}" >${cart.cartprice}</td>
<input type= "button" onclick= "Add (${cart.cartprice})"/>
Received in jquery:
var currentprice;
function Add (ID) {
currentprice=$ ("#" +id). Text ();
}
Another pattern, when the same loop body needs to invoke different DOM pairs with the ID object, modify the price name to
<TD id= "${cart.cartid}price" >${cart.cartprice}</td>

<input type= "button" onclick= "Add (${cart.cartprice})"/>
var currentprice;
function Add (ID) {
currentprice=$ ("#" +id+ "Price"). Text ();
}

Second, the servlet value to the JSP

The specific implementation is as follows:

Java code in servlet: String username = req.getparameter ("username");
Req.setattribute ("num", num);
Req.getsession (). SetAttribute ("Userphone", User.getuserphone ());
Request.getrequestdispatcher ("URL/jsp page"). Forward (Request,response);

JSP pages: Typically received with an EL expression

<%string s = (String) request.getattribute ("num");%>
such as ${num},${sessionscope.userphone} and other forms
The JSP page can take out the values of the servlet.

Third, servlet value to servlet
1. Direct jump (assuming that the variable num is already defined)
Resp.sendredirect ("taocan.do?username=" +username);

2, passing through the session, of course, application can also, omitted here.
Req.getsession (). SetAttribute ("Userphone", User.getuserphone ());
String username = (string) req.getsession (). getattribute ("username");

Summarize the difference between forward and Sendredirect:

1.request.getrequestdispatcher () is the request forwarding, the front and back page share a request, is running on the server side;
Response.sendredirect () is done by sending a command to the client's browser to jump to another new page.
So Requestdispatcher.forward () is "Transparent" to the browser;
and Httpservletresponse.sendredirect () is not.

Iv. JSP value to JSP
1, Direct transmission:
Set the properties on the first page:
<%
Request.setattribute ("name", "username");
Request.setattribute ("Date", "New Date ()");
%>
<jsp:forward page= "demo2.jsp"/>

In the second page, get:
<%
String username = (string) request.getattribute ("name");
Date date = (date) request.getattribute ("date");
%>

2, generally in the page rarely directly write the instruction code, another method for JS in the first pass the value to the middle servlet, and then to another page with the El expression directly obtained.

Summary: The connection and difference between the two

The "1" JSP is compiled into a servlet the first time it is run and resides in memory for invocation.
"2" JSP is a Web development technology, Servlet is a server-side application of small programs, when we visit a JSP page, the server will turn this JSP page into
After the Servlet applet runs, the results are fed back to the client's browser.
The "3" servlet corresponds to a control layer to invoke the corresponding JavaBean processing data, and finally returns the result to the JSP.
The "4" servlet is primarily used for steering, turning requests to the appropriate JSP pages.
"5" JSP more is the page display, servlet is more processing business, that is, JSP is a page, servlet is the way to implement JSP.
The "6" servlet can implement all the functions of the JSP, but since the artist uses the servlet to make the interface very difficult, the JSP was later developed.
"7" JSP technology development Site two modes: JSP + javabean;jsp + Servlet + JavaBean (generally in multi-tier applications, JSP is mainly used as the presentation layer
, and the servlet is used as the control layer, because too much code in the JSP is not conducive to maintenance, and this is left to the servlet to implement, and a lot of duplicate code is written in
JavaBean).
"8" The difference between the two is that the development interface is JSP directly can be written.
For example, write the table tag in the JSP: <table>[data]</table>;
The servlet needs to join: Out.println ("<table>[data]</table>").
JSP files are compiled into servlet files after they have been called by the application server (for example: Tomcat, Resin, WebLogic, and WebSphere). That is
It says that the servlet file is actually displayed on the Web page. Tomcat the resulting servlet file after compiling the JSP file is placed under the work folder, in the JSP
The HTML code is out in the servlet, and the JSP code is placed in a different location depending on the label.
The HTML code is embedded in the "9" JSP, and the Java code is embedded in the servlet.
"10" in a standard MVC architecture, the servlet takes a user request as a controller and forwards it to the corresponding action handler, which is primarily used as a view to produce
Dynamic page, EJB implements your business code as model.

JSP and servlet values and comparisons

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.