java自学==servlet

servlet连接数据库,在配置文件中填写servlet的初始参数

项目结构图

java自学==servlet

package servletAPI;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InitServlet extends HttpServlet {

	private String driver = "";
	private String URL = "";
	private String username = "";
	private String password = "";

//	获取web.xml中的init-param参数
	public void init() throws ServletException { //自动执行
		driver = getInitParameter("driver"); //参数"driver" 为param-name,返回的是param-value
		URL = getInitParameter("URL");
		username = getInitParameter("username");
		password = getInitParameter("password");
	}
//获得数据库连接的方法
	public Connection getConnection() {
		Connection con = null;
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(URL, username, password);
			System.out.print("2");
			return con;
		} catch (Exception e) {
		}
		return con;
	}
//通过doGet()方法实现数据查询功能
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=gb2312"); //指定内容类型,可显示中文
		PrintWriter out = response.getWriter(); 
//取得要在响应中输出的文本流
		Connection con = this.getConnection();
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("select * from Loginuser");
			while (rs.next()) {
				out.print(rs.getString("username"));
				out.print("    ");
				out.print(rs.getString("password"));
				out.print("    ");
				out.print(rs.getString("gender"));
				out.print("    ");
				out.print(rs.getInt("age"));
				out.print("    ");
				out.print(rs.getDate("logintime"));
				out.print("<br>");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
	
	<!-- 配置servlet -->
	<servlet>
	<!--servlet名称,必须唯一-->
		<servlet-name>firstservlet</servlet-name>
		<!--对应的servlet类-->
		<servlet-class>firstservlet.FirstServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>firstservlet</servlet-name>
		<!--访问的URL地址: http://localhost:8080/servlet/firstservlet-->
		<url-pattern>/[color=red]firstservlet[/color]</url-pattern>
	</servlet-mapping>

	<!-- 配置servlet -->
	<servlet>
		<servlet-name>testservlet</servlet-name>
		<servlet-class>servletAPI.TestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>testservlet</servlet-name>
		<url-pattern>/testservlet</url-pattern>
	</servlet-mapping>
	
	<!-- 配置servlet -->
	<servlet>
		<description>
			This is the description of my J2EE component
		</description>
		<display-name>
			This is the display name of my J2EE component
		</display-name>
		<servlet-name>initservlet</servlet-name>
		<servlet-class>servletAPI.InitServlet</servlet-class>
		<!-- 连接数据库的初始化参数 -->
		<init-param>
			<param-name>driver</param-name>
			<param-value>
				com.microsoft.jdbc.sqlserver.SQLServerDriver
			</param-value>
		</init-param>
		<init-param>
			<param-name>URL</param-name>
			<param-value>
				jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=login
			</param-value>
		</init-param>
		<init-param>
			<param-name>username</param-name>
			<param-value>sa</param-value>
		</init-param>
		<init-param>
			<param-name>password</param-name>
			<param-value>sa</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>initservlet</servlet-name>
		<url-pattern>/initservlet</url-pattern>
	</servlet-mapping>
	
</web-app>


第二个例子:
package firstservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {
	
	private int stringtoint(String str)throws NumberFormatException{
		int i = Integer.parseInt(str);
		return i;
	}

	//覆盖父类的方法doGet,doPost,  doPost不会显示传递的参数
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		
		String str;
		int inputNum;
		String outputstring;
		
		
		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		//输出网页
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY background=b01.jpg>");
		out.println("<h1>显示异常处理的servlet例子</h1>");
		str = request.getParameter("txtInputNum");
		
		try {
			inputNum = stringtoint(str);
			outputstring = "你输入的数字为:"+inputNum;
		} catch (NumberFormatException e) {
			outputstring = "对不起,输入不是数字";
		
		}
		out.println(outputstring);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
		
		
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		doGet(request,response);
	}
}


index.jsp

<%@ page language="java" contentType = "texthtml;charset=gb2312"%>
<%
[color=red]String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";[/color]%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    [color=red]<base href="<%=basePath%>">[/color]
    
    <title>servlet处理异常</title>
  </head>
  <body>
   <form action="[color=red]firstservlet[/color]"> 
			请输入一个数字: 
			<input type="text" name="txtInputNum" />
			<input type="submit" value="submit" />
		</form> 
  </body>
</html>

firstservlet对应于web。xml中的红色的url的/后的


全部源码见附件

你可能感兴趣的:(java,sql,xml,servlet,Microsoft)