Learn some of the problems Java needs to know

Source: Internet
Author: User
Tags define file upload html page http request implement sql net access
Question one, prepare the article

1 What is the Java EE? What is the difference between it and normal Java?
A: Java EE is all called Java2 Platform, Enterprise Edition.
"The Java-EE platform is essentially a distributed server application design environment--a java-based environment that provides:
• A running infrastructure environment for host applications.
• A set of Java extension APIs to create applications. "(from the Java server-side advanced programming)

2 Is it easy to learn?
A: Java EE is a collection of many technologies and is still growing.
You will encounter many proper nouns: for example (X) Html,servlet/jsp,jdbc,jms,jndi,ejb,xml,web Service ....
In particular, XML and Web service are growing fast. Fortunately, you don't have to wait until you learn all the techniques before you start programming.
Generally speaking, Java EE can be divided into 3 main applications: Servlet/jsp,ejb,xml/web Service and some support technologies such as JDBC and Jndi.
You can learn by one.

3 What's the use of Java?
A: Used to build large distributed enterprise-class applications. Or, in more fashionable terms, "e-business" applications.
These enterprises may be large to have a central database server, Web server clusters and office terminals all over the country, it may be small to just want to do a website.

4 Do you have a future in learning Java?
A: There is only one technology in this market that can compete with Java EE, and that is Microsoft's. NET.
Relatively speaking. NET to "new" some and the Java-ee to "old" some.
But. NET can only be used on Windows platforms (Microsoft claims to develop C # virtual machines on Linux but has not yet honoured that promise),
Given the momentum of Linux development, you can believe it. NET does not eminence.

5 It is said that the performance of the Java EE is not as good as. NET, is it true?
A: Microsoft claims to be less good than the same. NET program in the pet store, a sample program provided by Sun.
Sun countered that the program did not really reflect the performance of Java EE and blamed Microsoft for optimizing the database.
The author has not studied. NET therefore cannot jump to assert.
In any case, performance bottlenecks in large distributed programs usually come first from the wrong design.

6 Listen to you say so much, I want to learn to play.
A: Don't waste your time unless you want to eat it as a meal or as a technical reserve.
Flash has a lot to play with. Computer games are much more fun.

7 Learn how to start the Java EE?
A: First, download a Java EE server. Second, go to java.sun.com to download the Java EE API. Third, find a good reference book. Finally, find a handy IDE.
Java server. You can use Sun's Java EE SDK (free), or WebLogic (the best performance, but too large, and the author does not recommend piracy), or JBoss (free, is too little documentation), or JRun (development version free, the author uses this). The reference author feels Wrox's "Java Server-side advanced programming" is good, but too old (the author is in the 2001 Chinese version). You also need to download some of the latest technical data (certainly in English).
IDE If your machine is well configured (memory at least 512M, 256M or below do not consider), you can use IBM's Wsad, or continue to use eclipse or other.
You can also often go to the Java version of the water wood Tsinghua, but before posting to see if the essence of the area you want to answer.

8 I got down a Java server but not configured.
A: Please read the random guidance document carefully, different server configuration is not the same, the author helpless.

9 I found out you didn't mention Tomcat.
A: Tomcat is just a Web server, to be more precise, mostly just a web Container.
If you want to learn about EJBS, Tomcat doesn't meet your needs.

Second, servlet/jsp article

10 What is a servlet?
Answer: A servlet is a Java class. It handles HTTP (s) requests and responds, including returning an HTML page or handing it over to another URL.
The servlet must be running in a web container such as Tomcat.
The servlet must be a javax.servlet.http.HttpServlet subclass,
You can inherit the Doget () or Dopost () method, which corresponds to the GET request and POST request in HTTP (s) respectively.

11 How do I get the parameters in the HTTP request?
Answer: HttpRequest's GetParameter () method. For example: String paramvalue = Request.getparameter ("paramname");

12 How do I return the results?
A: You can use the relevant APIs to open an output stream and write directly to an HTML page into the stream.
But the author simply does not approve of doing so. On the one hand this will be very wordy.
On the other hand, from the point of view of the Model-view-controller model (which is classified as Front Controller mode in the Java EE Core model),
You should provide some HTML or JSP as a view, and the servlet will decide which view to go to depending on the request parameters.
You can use Response.sendredirect (...) method or Request.getdispatcher (...). Forward () method to implement.

What is the difference between sendredirect () and forward ()?
A: Sendredirect () is to send a redirect notification to the browser, and the browser redirects to the new URL.
The forward is a direct go to the new URL on the server side, transparent to the browser.
The former browser's address bar displays a new URL, and the address bar of the browser displays the servlet URL.
As a result, when the target URL is refreshed automatically, there are some differences.

14 I wrote a servlet program, how to run it?
A: The development of the Java EE program has a deployment (deploy) concept, is actually the development-deployment-run trilogy.
Most servers support hot deploy. You only need to be in the corresponding application directory (the specific path depends on the server) below
Create a directory that matches the war or ear format (see 16,17) and start the server, which can be accessed through the browser.
In particular, your servlet's class file should be placed in the/web-inf/classes directory.
Note that the Java EE SDK does not support hot deploy, you need to deploy it through its deploy tool.
Tomcat supports only the war format.

What is the difference between an ear and a war?
A: The ear is a complete Java EE application, including Web Parts and EJB parts.
War is just one of the Web Parts.

What is the ear format?
A: An ear can contain any number of war or EJB jars and contain a meta-inf directory.
A application.xml is included in the/meta-inf, which describes which modules the ear contains, as well as the security configuration.
Please see the reference book for details.

What is the war format?
A: A war contains a Web-inf directory, which contains the classes directory, the Lib directory, and the Web.xml.
/web-inf/classes Store class files by package,/web-inf/lib directory to store jar files,
Web.xml describes a lot of things, please read the reference books.

18 Where should my normal HTML file be placed?
A: Put it in a place other than/web-inf.

19 I can't access the servlet, not even the HTML file!
A: First you did not start the server. The second you hit the wrong port. Third, you did not configure Context-path correctly.
Four your server does not support auto reload or you turn off this option, you have to reboot the server.
Check that you did not put HTML in the/web-inf directory, which is not accessible.

20 I can access HTML but cannot access the servlet.
A: Please check your web.xml file. Make sure you define the <servlet> and <servlet-mapping> elements correctly.
The former identifies a servlet, which maps a URL relative to Context-path to a servlet.
In Tomcat you can access the servlet in/context-path/servlet/package/servletname form,
But this is just a convenient way for Tomcat to access, not a formal specification.
Please see the reference book for details.

21 What is JSP? What's the difference between it and the servlet?
A: You can treat JSP as an extensible HTML.
Although in essence JSP files are automatically translated by the server into the appropriate servlet.
It can be said that the servlet is for the Java programmer and the JSP is for the HTML programmer, except that the functions are completely equivalent.

22 My JSP shows the characters are garbled.
A: At the beginning of your JSP add a line <%@ page contenttype= "text/html; charset=gb2312 "%>
If you've already declared page I think you know how to modify it.

Where do you store your JSP files?
A: Except for any place under/web-inf.

24 How to reference Java beans inside the JSP.
A: First, make sure that the class you are referencing is under/web-inf/classes or in a jar within/web-inf/lib.
Second, in the JSP add a line <jsp:usebean id= "..." scope= "..." class= "..."/>
Please see the reference book for specific explanation.

25 I want to pass data between the servlet.
Answer: Use session. In servlet/jsp, you can save data in 4 places.
1 pages, this page.
2 session, used to store customer-related information, such as shopping cart, the corresponding interface for javax.servlet.http.HttpSession.
The session mechanism is actually an abstraction of the cookie and URL rewrite, and the server is implemented automatically using cookies or URL rewrite.
3 request, you can forward () when the message, the corresponding interface for Javax.servlet.http.HttpRequest.
4 application, or the context, storing global information, corresponding interface for Javax.servlet.ServletContext.

26 How do I call cookies?
A: The author suggests using session, you will always encounter some users who disable cookies. The session is then automatically implemented using URL rewriting.

27 How to implement file download in JSP?
A: Actually this is an HTML problem. The answer is a hyperlink <a>.

28 How to implement file upload?
A: The client is an HTML problem, set method in form to Post,enctype for Multi-part/form-data, plus a <input type= "file" >.
And in the received servlet is just an I/O problem.

29 I want the page to automatically refresh, such as chat room.
A: This is an HTML problem, in the This is called the Clinet-push, client refresh technology.

30 I want to let the user log in to access the page.
A: Use declarative security.
You only need to define the security role in Web.xml, and define a protected URL collection that can only be accessed by specific roles.
Most servers support database based user mappings, and you can just create two tables in the database and configure the server.
Note the Java EE SDK does not support database based user mappings.
See reference books and server documentation for details.

31 I want to be able to register users.
Answer: see 30. Write database operations are performed in the servlet that accepts the registration request.

32 How to access the database in the JSP?
A: The standard approach is to use the DAO pattern to define a Java bean to access the database and use it in the JSP.
However, when your database schema is simple, you can use the <sql:query> tags in jstl to quickly access them.

33 What is JSTL?
Answer: JSTL is the abbreviation of JSP Standard Tag Library. This is a common set of tags and will become part of JSP 2.0.
It contains assignment <c:set&gt, branch <c:if&gt, loop <c:foreach&gt, query database <sql:query&gt, update database <sql:update>
Wait You now need to add jstl like a custom tag library, but you can expect JSP 2.0 to take Jstl as part of it.
The tag library can be downloaded at http://jakarta.apache.org. Note that Jstl needs to run under a container that supports JSP1.2 or later.
The Help file can read Sun's JSTL formal specification.



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.