The difference and connection of JSP servlet

Source: Internet
Author: User

Simply put, Sun first developed the servlet, its function is relatively strong, the system design is also very advanced, just, it output HTML statement or use the old CGI way, is a sentence output, so, writing and modifying HTML is very inconvenient.    

JSP is essentially a servlet, but the two are created differently.

servlet is completely java program Code composition, good at process control and transaction processing, it is not intuitive to generate dynamic Web pages through Servlets.

JSP is made up of HTML code and JSP tags, which makes it easy to write dynamic Web pages.
So the servlet is used to control the business process in practical application , and the JSP is used to generate the Dynamic Web page.

In the struts framework, the JSP is in MVC Design mode of the view layer, while the servlet is located in the control layer.

JSP is the extension of servlet technology, which is essentially a simple way of servlet.

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. And JSP is the combination of Java and HTML into a file with a. jsp extension.

JSPs focus on Views, and Servlets are used primarily for control logic.

See a simple example of Jsp+servlet digestion: reprinted from: http://hi.baidu.com/wy521ly/blog/item/0523092af322b19d033bf648.html

Simple Jsp+servlet Instance 2008-09-02 20:24

Development environment MYECLIPSE+TOMCAT5

Create Web project first, project name test,

Create a login.jsp file under 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

Writing Acountbean.java files

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;
}

}

Writing a servlet class 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

Write session.jsp file after successful jump

<body>
<%
Com.ht.servlet.AcountBean account= (Com.ht.servlet.AcountBean) Session.getattribute ("account");
%>
Username: <%=account.getusername ()%>
<br>
Password:<%=account.getpassword ()%>
</body>

Jump when writing fail.jsp file fails

<body>
Logon Failed <br>
</body>

The following 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 EE component</description>
<display-name>this is the display name of my EE 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>

Run configuration via MyEclipse tomcat here omitted ...

http://127.0.0.1:8080/tset/Verification Results

The difference and connection of JSP servlet

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.