Differences between JSP and Servlet

Source: Internet
Author: User

Simply put, Sun first developed the servlet, which has powerful functions and advanced system design. However, it still uses the old CGI method to output HTML statements, therefore, it is inconvenient to write and modify HTML. Later, Sun introduced an embedded JSP similar to ASP and embedded the JSP tag into an HTML statement. This greatly simplifies and facilitates the design and modification of webpages. New Network languages such as ASP, PHP, and JSP are embedded script languages.


JSP is essentially a servlet, but the two are created in different ways.

Servlet is completely composed of Java program code. It is good at Process Control and transaction processing. It is not intuitive to generate dynamic web pages through servlet.

JSP consists of HTML code and JSP tags, allowing you to easily write dynamic web pages.
Therefore, in actual applications, servlet is used to control business processes, while JSP is used to generate dynamic web pages.

In the Struts framework, JSP is located at the view layer of the MVC design mode, while servlet is located at the control layer.

 

JSP is an extension of Servlet technology, essentially a simple servlet method.

After JSP compilation, it is "servlet-like ".

The main difference between Servlet and JSP is that the application logic of servlet is in the Java file and is completely separated from the HTML in the presentation layer. JSP combines Java and HTML into a file with the extension. jsp.

JSP focuses on views. servlet is mainly used for control logic.

Look at a simple JSP + servlet example, Digest: Reproduced from: http://hi.baidu.com/wy521ly/blog/item/0523092af322b19d033bf648.html

Simple JSP + servlet instance

Development Environment: myeclipse + atat5

Create a web project named test,

Create a login. jsp file in the webroot directory

Login. jsp

<Body>
<Form action = "login">
Username: <input type = "text" name = "username"> <br>
Password: <input type = "password" name = "PWD"> <br>
<Input type = "Submit">
</Form>
</Body>

Com. Ht. servlet in the SCR directory

Compile the acountbean. Java File

Package com. Ht. servlet;

Public class acountbean {
Private string username = "";
Private string Password = "";
Public String GetPassword (){
Return password;
}
Public void setpassword (string password ){
This. Password = password;
}
Public String GetUserName (){
Return username;
}
Public void setusername (string username ){
This. Username = username;
}

}

Compile the servlet checkaccount. Java File

Package com. Ht. servlet;

Import java. Io. ioexception;
Import java. Io. printwriter;

Import javax. servlet. servletexception;
Import javax. servlet. http .*;

Public class checkaccount extends httpservlet {

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Httpsession session = request. getsession ();
Acountbean account = new acountbean ();
String username = request. getparameter ("username ");
String Pwd = request. getparameter ("PWD ");
Account. setusername (username );
Account. setpassword (PWD );
If (username! = NULL) & (username. Trim (). Equals ("jsp "))){

If (PWD! = NULL) & (PWD. Trim (). Equals ("1 "))){

System. Out. println ("session ");

Session. setattribute ("Account", account );
String logon_suc = "session. jsp ";
Response. sendredirect (logon_suc );
Return;
}
}
String logon_fail = "fail. jsp ";
Response. sendredirect (logon_fail );
Return;
}

Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

Doget (request, response );
}

}

Under the webroot directory

Jump to the session. jsp file after successful compilation

<Body>
<%

Com. Ht. servlet. acountbean account = (COM. Ht. servlet. acountbean) Session. getattribute ("account ");

%>
Username: <% = Account. GetUserName () %>
<Br>
Password: <% = Account. GetPassword () %>
</Body>

Jump after failed to write fail. jsp file

<Body>
Logon Failed <br>
</Body>

Below is the Web. xml file

<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Welcome-file-List> <welcome-File> login. jsp </welcome-File> </welcome-file-List>


<Servlet>
<Description> This is the description of my J2EE component </description>
<Display-Name> This is the display name of my J2EE component </display-Name>
<Servlet-Name> checkaccount </servlet-Name>
<Servlet-class> com. Ht. servlet. checkaccount </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-Name> checkaccount </servlet-Name>
<URL-pattern>/login </url-pattern>
</Servlet-mapping>
</Web-app>

Running and configuring Tomcat through myeclipse is omitted here...

Http: // 127.0.0.1: 8080/tset/verify and view the result

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.