A servlet sees a few mistakes in getting started with Java

Source: Internet
Author: User

After installation of the Java environment, CMD-JAVAC error-------------"Environment variables are wrong, and finally fully equipped with system variables, OK

Can waste a day's time

Write one of the simplest servlets, tomcat error 404

See a

 in production environments is not found on the Java.library.path:d:\program files\jdk\bin; C:\Windows\Sun\Java\bin; C:\Windows\system32; C:\windows;d:\program Files\jdk\jre\bin; C:\Program Files (x86) \intel\icls client\; C:\Program Files\intel\icls client\; C:\Pro

I think it's the reason, it's not really important

It's important not to find index.jsp-----------------------file location, directory wrong

Can waste a day's time

Servlet read remote Orcale, error-----------------------------"Java.lang.ClassNotFoundException: Oracle.jdbc.driver.OracleDriver

The JDK and the driver are matched, checked.

The variable is also added

In fact, the webcontent\web-inf\lib didn't put the jar in the bag.

Can waste a day's time

Does the Java connection Orcale need to install the client? ----------------------------"not required, in fact there is a driver is OK, the driver is a small client

Can waste a day's time

Import javax.servlet.*; Error---------------"Reason, no build Servlet-api.jar

Outline Catalogue

XML configuration

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">  <Display-name>Fleam</Display-name>  <welcome-file-list>    <Welcome-file>Index.html</Welcome-file>    <Welcome-file>Index.htm</Welcome-file>    <Welcome-file>index.jsp</Welcome-file>    <Welcome-file>Default.html</Welcome-file>    <Welcome-file>Default.htm</Welcome-file>    <Welcome-file>default.jsp</Welcome-file>  </welcome-file-list>      <!--read by Getservletcontext (). Getinitparameter ("XX") -<Context-param>   <Param-name>Driver</Param-name>   <Param-value>Oracle.jdbc.driver.OracleDriver</Param-value></Context-param><Context-param>   <Param-name>Url</Param-name>   <Param-value>Jdbc:oracle:thin: @XXXXXXXXXXXXXXXXX: 1521:xxx</Param-value></Context-param><Context-param>   <Param-name>Username</Param-name>   <Param-value>Xxx</Param-value></Context-param><Context-param>   <Param-name>Password</Param-name>   <Param-value>xxxxx</Param-value></Context-param>    </Web-app>

Program

 PackageHelloWorld;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.json.JSONException;ImportOrg.json.JSONObject;ImportJava.io.PrintWriter;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;/** */@WebServlet ("/helloworld") Public classHelloWorldextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; PrivateString Diverclass; PrivateString UserName; PrivateString password; PrivateString URL; /*** Default constructor. */     PublicHelloWorld () {//TODO auto-generated Constructor stub        }    /**     * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/@Overrideprotected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Jsonobject Jsonobject=NewJsonobject (); Response.setcontenttype ("Text/html"); Response.setcharacterencoding ("GB2312"); PrintWriter out=Response.getwriter (); Diverclass=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("Driver"); UserName=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("username"); Password=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("Password"); URL=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("url"); Try{class.forname (diverclass); //conn = Drivermanager.getconnection ("Jdbc:mysql://localhost/bbs?user=root&password=12345678 ");Conn=drivermanager.getconnection (URL, username,password); stmt=conn.createstatement (); RS= Stmt.executequery ("SELECT * from ANIMALS");  while(Rs.next ()) {Try{jsonobject.put ("Name", Rs.getstring ("name")); } Catch(jsonexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                  //out.println (rs.getstring ("NAME"));out.println (Jsonobject); }                                                                    } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } finally {            Try {                if(rs! =NULL) {rs.close (); RS=NULL; }                if(stmt! =NULL) {stmt.close (); stmt=NULL; }                if(Conn! =NULL) {conn.close (); Conn=NULL; }            } Catch(SQLException e) {e.printstacktrace (); }        }    }    /**     * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {printwriter out=Response.getwriter (); Out.println ("<div style= ' width:100px;height:100px;background-color:red ' >123123</div>"); }}

A servlet sees a few mistakes in getting started with Java

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.