Java EE Learning Notes

Source: Internet
Author: User

about Servlet

The servlet specification is an important part of the Java EE Technical Specification, which is a server-side Java application independent of the Platform and protocol that can generate dynamic Web pages (in fact, Servlets are not just for pages that return HTML, for example, Servlets can also return strings in formats such as Xml,json, and, of course, can return video streams, and so on.
The servlet will run on the servlet container, and currently, IBM's Websphere,bea Weblogic,apache Tomcat,sun glassfish, these Java EE servers, all contain servlet containers, I want to say that the relationship between the servlet's container and the Java EE server is different. In the case of Tomcat, it is a well-known open source Java EE server that contains a servlet container (but does not contain the EJB container), which means that Tomcat is a container that does not fully support the Java EE specification (EJB is not supported). But this is enough to build a Web site. If you want to use Tomcat and use EJBS, it is usually using JBoss as a server, because JBoss itself is a container for EJBS and integrates Tomcat to complete the role of the servlet container. In addition to providing a servlet container, Tomcat obviously has to assume the role of HTTP server, but some people use the Apache server +tomcat servlet container to form a complete server, that is to say, Instead of using the HTTP service provided by Tomcat, use Apache.
Sun's GlassFish is a relatively complete server that supports the Java EE specification. Just now saw a news, oracle after the acquisition of Sun, will still support GlassFish. The original text reads as follows:

( GlassFish
Oracle says it will adjust the portfolio of Oracle WebLogic Server and GlassFish, and they plan to keep GlassFish Enterprise server as an open-source ri that adheres to the Java EE specification. At the same time, Oracle says it will continue to support the GlassFish community.
Editor's note: In addition to the two products that developers are concerned about (yes, there is also Java FX.) For Java,oracle has been clear since the start of the acquisition, but Java FX, which is still not mentioned in this FAQ, also mentions Sun's virtualization products and the processing strategy of open office. )

I think that servlet technology is quite similar to CGI technology. If we put the servlet in a traditional CGI program, and in the middle of the present jsp,jsf,asp.net, I think it is closer to the former, without an essential leap.

Detailed servlet instances

The above link, well written, OK:
Suppose I write a helloservlet, it returns a welcome page.
How did the HelloServlet be created? First of all, it's not new, and the container doesn't know what the name of the servlet classes will be. This is like MFC, who knows what our document class is called, so dynamic creation is useful. Of course, in Java, Reflection is a more powerful weapon. (And remember, the container won't be new helloservlet, and that's what we're trying to call this servlet, not new!)
The server listens on one port, say 8080, and now the browser is going to visit our server, Http://127.0.0.1:8080/hello.
Ok,server will accept the link of the browser, of course, this is a TCP layer problem, and at the HTTP level, we will see the request HTTP requests information, which contains the URL, so we naturally get the string hello. At this point, the HTTP server will have to transfer the processing logic to the servlet container.
According to the Java EE specification, the servlet container reads a web. Xml. There are roughly such configuration content:

<servlet>        <servlet-name>Hello</servlet-name>        <servlet-class> org.bizcuit.helloservlet</servlet-class>    </servlet>    <servlet-mapping>        < servlet-name>hello</servlet-name>        <url-pattern>/hello</url-pattern>    </ Servlet-mapping>

Note that the key four lines, where Servlet-name is Hello, become a bridge above and below the two sections, the above section illustrates which Java class is instantiated by the servlet that invokes the name. The following section shows that if the pattern of the URL is/hello, a servlet named Hello is called. Among them, also pay attention to Url-pattern item, here is some name purpose.
OK, so that the servlet container knows which Servlet's class to load, then the load is done by reflection.

Of course, this is simpler than pseudocode, because a servlet class typically produces only one instance within a container (there are ways to generate multiple instances, but it can produce efficiency problems, later), so be aware of thread-safety issues (see later).
But now that we have an instance of the servlet, we can call its method, and according to the servlet specification, the container will call the service, or the Doget,dopost method, according to the browser's request (get/post/...). Take the Doget method as an example:

public class HelloServlet extends HttpServlet {public    void doget (HttpServletRequest req, HttpServletResponse resp) C2/>throws IOException {        resp.setcontenttype ("text/html");        Resp.getwriter (). println ("<div>Hello</div>");}    }

Note the two parameters of the Doget method, it is very important, here is not to repeat, or to be mastered in concrete practice.
The session can be obtained by this method. Req.getsession ();

Introduction to JSP
I personally think that JSP is a more flexible than PHP, a powerful server-side script, but PHP is really a lot easier ah.

JSP (Java Server pages) is a dynamic Web technology standard developed by Sun, a company that many companies are involved in. JSP technology is a bit like asp,php technology, it is in the traditional Web page HTML file (*.htm,*.html) inserted Java program segment (scriptlet) and JSP tag (tag), thereby forming a JSP file (*.jsp).
It is also necessary to run the JSP as a servlet container, because the JSP will be translated into a servlet when it is first accessed, of course, The servlet will eventually be compiled into a. class file, so the JSP is built on the technology of the servlet, and the JSP is running fast, after all, is compiled into bytecode in the run, compared to the traditional ASP script interpretation way, a lot faster.
JSP, and the JSP tag can be used as a separate topic to study.

<JSP Test Part >

1. DOWNLOAD Tomcat First:tomcat 6.x zip format
After downloading, unzip.
2. Start Tomcat
Locate the directory $tomcat_install_path\apache-tomcat-6.0.20\bin,windows, and then double-click Startup.bat.
http://localhost:8080/
At least try Tomcat is not really up. go~
3. Create a JSP file.
Catalogue, $TOMCAT _install_path\apache-tomcat-6.0.20\webapps\root
Create a file, hello.jsp. Enter the following content.

<% @page language= "java"%><% @page import= "java.util.*"%>

Save, and then start the server (of course, after the server up, and then create, modify the JSP can also, do not need to restart the server.) ), give it a try, http://127.0.0.1:8080/hello.jsp.
4. Change again, experience the jsp,<%%>, you can accept Java code (of course, not all).

<servlet Test Part >
Write a simple JSP, I do not think the IDE is OK, but servlet, we still use the IDE, large sections of Java code, I like to use Eclipse.
1. Download Eclipse-jee-galileo-sr1-win32,click here to download

2. Start Eclipse. Note that eclipse must specify a workspace to be done.

3. From the menu to find File->new->project, choose to pop up a project can create a dialog box. Find Web |_ Dynamic Web Project inside.
Irresponsible to say, you go all the way next. There should be no problem. The name of the project can be selected as Hello.

4. Create a second server.

Irresponsible to say, find the dialog box, you go all the way next. There should be no problem.

5. This is almost the case, you can write the code, you can debug it. The specific problem should not be difficult to solve. Create a servlet inside the Hello project.
In the package Explorer on the left, expand Hello, right-click in the SRC directory, prepare to create a servlet, find the dialog box,


Write HelloServlet in class name, and Java package writes Org.bizcuit. The rest is not modified. OK, so the IDE will help us create the Java file for the good one servlet.
Within the Doget method, you can try to write code like this:

PrintWriter pw = Response.getwriter ();
Pw.print ("

Of course, the return of HTML in the servlet is often a comparison of the soil, because it is not as good as writing JSP (of course, the servlet returned to HTML may have a better reason.) )
6. Start Debugging.
Right-click on Project Hello and locate the debug as menu item and select Debug on server.
Select an existing server (Choose an exsiting server.). Because we've built a server tomcat.
Then go all the way next to finish~. Try Http://127.0.0.1:8080/Hello/HelloServlet.
In the URL, the first Hello is because the project name you created is Hello, next HelloServlet, is the name of your servlet class.

7. Try rewriting your servlet, no need to restart the server.

Java EE Learning Notes

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.