1、 准备工作
工具:tomcat、editplus、jdk、windows操作系统
操作:在windows操作系统上安装jdk、tomcat、editplus,配置JAVA_HOME,Path,CLASSPATH;在CLASSPATH路径上加上servlet-api.jar
CLASSPATH D:\Program Files\Java\jdk1.6.0_10\lib;F:\tomcat\lib\servlet-api.jar;
也可以配置CATALINA_HOME F:\tomcat
2、tomcat、servlet简介
tomcat是完全免费得web服务器
servlet容器也叫servlet引擎,是web服务器或应用程序服务器的一部分,用于在发送的请求和响应之上提供网络服务,解码基于MIME的请求,格式化基于MIME的响应
解压tomcat后,得到如下目录,当然tomcat7中只有一个lib目录。其中要用到的servlet-api.jar放在lib目录下
servlet生命周期
3、java web应用程序
一个web应用程序是由一组servlet、类、html页面以及其他资源组成的运行在web服务器上的完整的应用程序。一个Context表示一个应用程序
以一个登陆servlet为例介绍java web程序的开发流程,完成功能:用户通过login.html登陆,登陆成功返回客户端和服务器的一些信息,以及请求信息
4、目录结构
F:\tomcat\webapps\servlet_demo这是我电脑上的应用程序servlet_demo的路径,这个路径是上下文路径,在该路径下建立如下文件:
5、各个文件存放位置及源代码
lib目录下存放我们要用到的资源,这里存放的是mysql的jdbc驱动
src目录下放的是源代码,这个在部署的时候可以不需要的
DataBaseConnection类是一个通用的数据库连接工具类
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- public class DataBaseConnection {
- public static final String ACCESS_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
- public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
- public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
- public static final String SQLSERVER_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
-
- private Statement st = null;
- private Connection connection = null;
-
- public Statement getStatement(String driver, String url, String user,
- String password) {
- try {
-
- Class.forName(driver);
-
-
- connection = DriverManager.getConnection(url, user, password);
-
-
- st = connection.createStatement();
-
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return st;
- }
-
- public void close() {
- try {
- if (st != null)
- st.close();
- if (connection != null)
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
LoginServlet类用于处理表单数据,判断用户名密码是否正确,如果正确则返回信息,错误转到登陆界面
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import java.io.*;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Enumeration;
-
- public class LoginServlet extends HttpServlet {
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doPost(req, resp);
- }
-
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
- resp.setContentType("text/html;charset=utf-8");
-
- PrintWriter out = resp.getWriter();
- if (isRight(req, resp)) {
-
- String clientAddress = req.getRemoteAddr();
- int clientPort = req.getRemotePort();
- String clientHost = req.getRemoteHost();
- String serverAddress = req.getLocalAddr();
- int serverPort = req.getLocalPort();
- String serverHost = req.getLocalName();
-
-
- out.print("<html><head><title>");
- out.print("登陆成功!</title></head><body><center>");
- out.print("<p>客户机IP:" + clientAddress + " 客户机端口:" + clientPort + " 客户机主机名:" + clientHost);
- out.print("<p>服务器IP:" + serverAddress + " 服务器端口:" + serverPort + " 服务器主机名:" + serverHost);
-
-
- Enumeration headNames = req.getHeaderNames();
- out.print("<p>http请求信息<br><table border=\"1\">");
- while (headNames.hasMoreElements()) {
- String headName = (String) headNames.nextElement();
- String value = req.getHeader(headName);
- out.print("<tr><td>" + headName + "</td><td>" + value + "</td></tr>");
- }
- out.print("</table>");
-
-
- ServletContext context = getServletContext();
-
-
-
- Integer count = (Integer) context.getAttribute("count");
- if (count == null)
- count = 1;
- else
- count++;
- context.setAttribute("count", count);
- out.print("<p><br>当前访问量:" + count);
- out.print("</body></html>");
- } else {
-
-
-
-
-
-
-
-
-
-
-
- resp.sendRedirect("login.html");
- }
- out.close();
- }
-
-
- private boolean isRight(HttpServletRequest req, HttpServletResponse resp) {
-
- String user = req.getParameter("user");
- String password = req.getParameter("password");
-
- String driverClass = getInitParameter("driverClass");
- String url = getInitParameter("url");
- String username = getInitParameter("username");
- String pw = getInitParameter("password");
- DataBaseConnection dbConnection = new DataBaseConnection();
- Statement st = dbConnection.getStatement(driverClass, url, username, pw);
- try {
- ResultSet result = st.executeQuery("select * from user");
- while (result.next()) {
- if (result.getString("username").equals(user) && result.getString("password").equals(password))
- return true;
- }
- result.close();
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- dbConnection.close();
- return false;
- }
- }
登陆页面login.html
- <html>
- <head>
- <title>登陆</title>
- </head>
- <body>
- <center>
- <form action="login" method="post">
- 用户名:<input type="text" name="user" id="user"><p>
- 密码:<input type="password" name="password" id="password"><p>
- <input type="submit" value="登陆">
- <input type="reset" value="重置">
- </form>
- </body>
- </html>
数据库信息
6、运行
第5步弄完之后,打开tomcat和数据库,在地址栏输入:http://localhost:8080/servlet_demo 就会进入登陆界面
当然也可以通过telnet来运行:
先连接:
telnet localhost 8080
然后访问:
GET /servlet_demo/login HTTP/1.1
Host:localhost
连续按两下回车
7、打包成war文件发布
dos窗口切换到 F:\tomcat\webapps\servlet_demo 。执行命令:jar -cvf servlet_demo.war * 。在当前目录下就有了servlet_demo.war 文件,可以用压缩软件直接打开该文件,也可以用命令:jar -tf servlet_demo.war打开该文件。
删除webapps目录下得servlet_demo应用,把servlet_demo.war文件放入目录下,这样就部署好了,运行跟第6步一样了。。。
8、关于servlet中重定向的区别
9、参考资料
《java web开发详解》