关于servlet的教程请参考:Java Servlet完全教程
servlet代码示例如下(可以连接数据库mysql):
package com.net;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
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 com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
/**
* Servlet implementation class W3CSchool
*/
@WebServlet("/W3CSchool")
public class W3CSchool extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public W3CSchool() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// StringBuilder textSql = new StringBuilder();
// try {
// Class.forName("com.mysql.jdbc.Driver");
// System.out.println("Driver loaded ok");
//
// Connection connection = (Connection) DriverManager.getConnection(
// "jdbc:mysql://192.168.1.150/test", "teamtalk", "123456");
//
// Statement statement = (Statement) connection.createStatement();
// ResultSet resultSet = statement.executeQuery("select * from persion");
// while (resultSet.next()) {
// textSql.append(resultSet.getString(1) + " " + resultSet.getString(2) + "\n");
// }
// }
// catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// catch (SQLException e) {
// e.printStackTrace();
// }
// finally {
//
// }
response.setContentType("text/html");
PrintWriter out = response.getWriter();
FileReader file = new FileReader("h://html//w3cschool.html");
StringBuilder text = new StringBuilder();
char[] buff = new char[1024];
while (file.read(buff) > 0) {
text.append(buff);
}
file.close();
out.println(text.toString());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
/*
String username = request.getParameter("username");
String password = request.getParameter("password");
String maths = request.getParameter("maths");
System.out.println("hello " + username + "(" + password + ")");
System.out.println("maths " + maths);
*/
Enumeration params = request.getParameterNames();
while (params.hasMoreElements()) {
//System.out.println((String)params.nextElement());
String key = (String)params.nextElement();
String value = request.getParameter(key);
System.out.println(key + ": " + value);
}
//String[] infos = request.getParameterValues(name)
}
}
注意:在java项目中,只需要引入mysql-connector-java-5.1.7-bin.jar就可以运行java项目。
在web项目中,当Class.forName("om.mysql.jdbc.Driver");时myeclipse是不会去查找字符串,不会去查找驱动的。所以只需要把mysql-connector-java-5.1.7-bin.jar拷贝到tomcat下lib目录就可以了。
网页内容为:
<html>
<head>
<!-- 放置一些属性信息,辅助信息 -->
<!-- 这里可以引用其他的文件(css/js) -->
<!-- 这里面内容会先加载 -->
<title>html page</title>
</head>
<body>
<form action="" method="post">
username:<input type="text" name="username" value=""/> <br/>
password:<input type="password" name="password" value=""/>
<input type="submit" value="submit"/>
</form>
<form action="" method="post">
<input type="checkbox" name="maths"/> math
<input type="checkbox" name="physics" checked="checked"/> physics
<input type="checkbox" name="chemistry"/> chemistry
<input type="submit" value="submit">
</form>
</body>
</html>
</html>