由一个Servlet 看java入门常犯的几个错误
安装完java环境后,cmd-javac 报错 -------------》环境变量配错了,最后全配成系统变量,ok了
能浪费一天的时间
写一个最简单的Servlet ,tomcat报错404
看到一个
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was 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
以为是它的原因,其实都不重要
重要是没找到index.jsp -----------------------》文件位置、目录错了
能浪费一天的时间
Servlet 读取远程orcale,报错 -----------------------------》java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
jdk和驱动是匹配的,查过了
变量也加了
其实是webcontent\web-inf\lib里没有放jar包
能浪费一天的时间
java连接orcale需要安装客户端吗?----------------------------》不需要,事实上有一个驱动就ok了,驱动就是一个小客户端了
能浪费一天的时间
import javax.servlet.*;报错---------------》原因,没有build Servlet-api.jar
大纲目录
xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns: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> <!-- 通过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>
程序
package helloworld; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** */ @WebServlet("/HelloWorld") public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 1L; private String diverClass; private String userName; private String password; private String url; /** * Default constructor. */ public HelloWorld() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn = null; Statement stmt = null; ResultSet rs = null; JSONObject jsonObject = new JSONObject(); 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 block e.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(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<div style='width:100px;height:100px;background-color:red'>123123</div>"); } }