一些常用的Servlet小方法

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class HelloServlet extends HttpServlet {

	public HelloServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		//解决页面输出乱码问题 等于jsp页面 <%@page contentType="text/html;charset=GBK"%>
		//解决get方式提交和response.sentRedirect()方式
		resp.setContentType("text/html;charset=GBK"); 

		//解决post方式提交内容的乱码,一定要写在存取第一个参数之前
		req.setCharacterEncoding("GBK"); 
		//不要调用 resp.setCharacterEncoding("GBK");,此方法相当于resp.setContentType的后半部分charset=GBK
		//解决get方式乱码问题 修改server.xml -->URIEncoding="GBK"


		//拿到输出流对象
		PrintWriter out = resp.getWriter();
		

		//打印流的使用
		out.println("<html>中文乱码问题</html>");
		out.flush();//清除缓存
		out.close();

		//得到session
		HttpSession session = req.getSession();

		//其中为读取web.xml文件中的参数,进行转码
		String DBDRIVER = this.getServletConfig().getInitParameter("DBDRIVER");
		DBDRIVER = new String(DBDRIVER.getBytes("ISO8859-1"),"GBK");
			
		
		//取得上一个页面的参数
		//<input name="txtID" type="text"/>
		String txtID = req.getParameter("txtID").toString();
		


		//取得一个页面同名参数的值
		/*
		<input type="Checkbox" name="**inst" value="篮球">
		<input type="Checkbox" name="**inst" value="足球">
		<input type="Checkbox" name="**inst" value="实况">
		*/
		//枚举类型,历史遗留问题
		Enumeration enu = req.getParameterNames();
		while(enu.hasMoreElements())
		{
			//返回所有参数名称
			String name = (String)enu.nextElement();
			String temp[] = req.getParameterValues(name);
			if(temp.length==1)
			{
				String paramValue = temp[0];
			}
			else{
				//循环打印
			}
		}
		//通过Map来取得
		Map<String, String[]> paramMap = req.getParameterMap();
		//Set k = paramMap.keySet();
		Set<Map.Entry<String, String[]>> entries = paramMap.entrySet();
		for (Iterator<Map.Entry<String, String[]>> it = entries.iterator(); it.hasNext();) {
			Map.Entry<String, String[]> entry = it.next();
			String paramName = entry.getKey();
			out.print(paramName);
			String[] paramValues = entry.getValue();
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() == 0)
					out.println("No Value");
				else
					out.println(paramValue);
			} else {
				for (int i = 0; i < paramValues.length; i++) {
					out.println(paramValues[i]);
				}
			}
		}


		//取得客户端IP地址  
		req.getRemoteAddr()
	}

	public void init() throws ServletException {
	}
}


/*
<servlet>
	//配置映射
	<servlet-name>simple</servlet-name>
	<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>
	//程序运行优先级
	<load-on-startup>1</load-on-startup>
	//配置初始化参数
	<param-name>DBDRIVER</param-name>
	<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</servlet>
<servlet-mapping>
	<servlet-name>simple</servlet-name>
	<url-pattern>/demo</url-pattern>
</servlet-mapping>
*/

你可能感兴趣的:(oracle,xml,jsp,jdbc,servlet)