There is no nonsense: starting from 0 to develop yourself BS architecture with Java - 3.1

zhaozj2021-02-16  62

Article 3: Getting Started with Servlet

Serious warning: People who know the servlet should not look down

Tell me someone to tell you:

Servlet is the server-side Java program

The client is called by the HTTP protocol, forming a dynamic web page at the client

what do you need:

A file: servlet.jar

What, do you ask this JAR file? Use your WinRAR to open it, there is nothing, just to cover the sky .class. Don't take too much. In short, these .class gives you a lot of methods.

Why, did you find this servlet.jar? I don't want to tell you anything, do you do it yourself.

Write a program to see:

Built a project mythree under your Tomcat's WebApps, remember, what else does it need? ! At the same time, I will build a folder in your Mythree, give it a name, just call javasource.

Hurry to put servlet.jar in mythree --- Web-inf --- Lib inside Eclipe, newly build your project, also name mythree, easy to identify, put your source file (.java) In Mythree --- Javasource, put your class file (.class) in Mythree --- Web-INF --- Classes. (If you don't know how to get it, go back to watch Mars and Pluto.) In Eclipe, right-click "Java Build Path" in Eclipe - "Java Build Path" on the right side of "Libraries" ----- "Add External Jars" Enter, select the servlet.jar just placed under LIB. . .

Ok, write a class first, take the name: Test program code as follows: import java.io. *;

Import javax.servlet. *;

Import javax.servlet.http. *;

Public Class Test Extends Httpservlet

{

Public void doget (httpservletRequest RQ,

HTTPSERVLETRESPONSE RP)

THROWS IOEXCEPTION, ServletException

{

Rp.setContentType ("text / html; charSet = GB2312");

PrintWriter out = rp.getwriter ();

Out.println (" Welcome to use servlet ");

}

}

Change your content of this web.xml

123 test 123 / abc

Please launch your Tomcat again in your IE address bar: http: // localhost: 8080 / mythree / abc

Did you see the effect?

Why, I feel too boring, how is it a little complex?

The procedure just above is:

Import java.io. *;

Import javax.servlet. *;

Import javax.servlet.http. *;

Public Class Test Extends Httpservlet

{

Public void doget (httpservletRequest RQ,

HTTPSERVLETRESPONSE RP)

THROWS IOEXCEPTION, ServletException

{

Rp.setContentType ("text / html; charSet = GB2312");

PrintWriter out = rp.getwriter ();

Out.println ("");

Out.println ("

");

Out.println ("
");

Out.println ("
");

Out.println ("");

Out.println ("");

}

Public void dopost (httpservletRequest RQ,

HTTPSERVLETRESPONSE RP)

THROWS IOEXCEPTION, ServletException

{

Rp.setContentType ("text / html; charSet = GB2312");

PrintWriter out = rp.getwriter ();

String t = rq.getParameter ("t1");

Out.println (" Welcome:");

Out.println (T);

Out.println ("");

}

}

Please start your Tomcat again in your IE address bar: http: // localhost: 8080 / mythree / abc Enter your name in the box, then press "OK" to see. .

Just want to explain 2 points:

Look

Out.println ("
"); and

String t = rq.getParameter ("t1"); the following RQ.GetParameter ("t1") will find the NAME called T1 in Dongdong, and obtain its content assignment to T.

Look at Web.xml

123 test 123 / abc Here the test in the test is the same as the name of the class in your project.

Here / ABC ABC is the same as you entered later after Mythree on the IE address bar

Here 123 , above, below. These, have you found it? Now you know how to change / ABC to / CBA, do you have 123 to 321?

转载请注明原文地址:https://www.9cbs.com/read-22254.html

New Post(0)