//
you're reading...
Java Servlet

Java Servlets

LoginServlet.java

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

public class LoginServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
              throws ServletException,
                     IOException {
        String username = request.getParameter("username");   
        String password = request.getParameter("password");          	
        
        System.out.println("username=" + username);
        System.out.println("password=" + password);
        
        response.setContentType("text/html;charset=GB18030");
        
        response.getWriter().println("<html>");
        response.getWriter().println("<head>");	
        response.getWriter().println("<title>Login Information</title>");	
        response.getWriter().println("</head>");	
        response.getWriter().println("<body>");	
        response.getWriter().println("Welcome【" + username + "】User Login Successfully!!!");	
        response.getWriter().println("</body>");	
        response.getWriter().println("</html>");
    }                 	
  
	public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
              throws ServletException,
                     IOException {
      doGet(request, response);               	
	}                     
}

SearchStudentServlet.java

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

import com.bjpowernode.exam.model.*;
import com.bjpowernode.exam.manager.*;

public class SearchStudentServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		String sBeginDate = request.getParameter(&amp;quot;beginDate&amp;quot;);
		String sEndDate = request.getParameter(&amp;quot;endDate&amp;quot;);

		Date beginDate = new Date();
		Date endDate = new Date();
		try {
			beginDate = new SimpleDateFormat(&amp;quot;yyyy-MM-dd&amp;quot;).parse(sBeginDate);
		 	endDate = new SimpleDateFormat(&amp;quot;yyyy-MM-dd&amp;quot;).parse(sEndDate);
		 }catch(Exception e) {
			e.printStackTrace();
		 }	 

		StudentManager studentManager = new StudentManagerImpl();
		List&amp;lt;Student&amp;gt; studentList = studentManager.findStudentList(beginDate, endDate);

		//将学生列表设置到requet范围中
		//request.setAttribute(&amp;quot;student_list&amp;quot;, studentList);

		//转发,转发是在服务器端转发的,客户端是不知道的
		//request.getRequestDispatcher(&amp;quot;/student_list.jsp&amp;quot;).forward(request, response);

		//将studentList放到session中
		HttpSession session = request.getSession();
		session.setAttribute(&amp;quot;student_list&amp;quot;, studentList);

		//重定向,不会共享request
		//以下写法错误,该 &amp;quot;/&amp;quot;代表了8080端口
		//response.sendRedirect(&amp;quot;/student_list.jsp&amp;quot;);
		response.sendRedirect(request.getContextPath() + &amp;quot;/student_list.jsp&amp;quot;);
	}
}

SetCookies.java

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

public class SetCookies extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		Cookie c1 = new Cookie(&amp;quot;password&amp;quot;, &amp;quot;123&amp;quot;);
		response.addCookie(c1);

		Cookie c2 = new Cookie(&amp;quot;client_ip&amp;quot;, request.getRemoteAddr());

		//Set cookie life cycle to 1hour/per second
		c2.setMaxAge(60*60);
		response.addCookie(c2);

		response.getWriter().println(&amp;quot;SetCookies OK!&amp;quot;);
	}
}

SetSessionServlet.java

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

public class SetSessionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		session.setAttribute(&amp;quot;ip&amp;quot;, request.getRemoteAddr());

		response.getWriter().println(&amp;quot;SetSession OK!&amp;quot;);
	}
}

ShowCookies.java

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

public class ShowCookies extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		Cookie[] Cookies = request.getCookies();
		for (int i=0; i&amp;lt;Cookies.length; i++) {
			Cookie c = Cookies[i];
			response.getWriter().println(c.getName() + &amp;quot;, &amp;quot; + c.getValue());
		}
	}
}&lt;/pre&gt;
&lt;pre&gt;[/sourcecode]
<pre>&amp;amp;lt;h3&amp;amp;gt;ShowSessionServlet.java&amp;amp;lt;/h3&amp;amp;gt;</pre>

<pre>&amp;lt;h3&amp;gt;StudentServlet.java&amp;lt;/h3&amp;gt;</pre>
 import java.text.*; import java.util.*; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import com.bjpowernode.exam.model.*; import com.bjpowernode.exam.manager.*; public class StudentServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sBeginDate = request.getParameter(&amp;quot;beginDate&amp;quot;); String sEndDate = request.getParameter(&amp;quot;endDate&amp;quot;); Date beginDate = new Date(); Date endDate = new Date(); try { beginDate = new SimpleDateFormat(&amp;quot;yyyy-MM-dd&amp;quot;).parse(sBeginDate); endDate = new SimpleDateFormat(&amp;quot;yyyy-MM-dd&amp;quot;).parse(sEndDate); }catch(Exception e) { e.printStackTrace(); } //System.out.println(&amp;quot;sBeginDate=&amp;quot; + sBeginDate); //System.out.println(&amp;quot;sEndDate=&amp;quot; + sEndDate); StudentManager studentManager = new StudentManagerImpl(); List&amp;lt;Student&amp;gt; studentList = studentManager.findStudentList(beginDate, endDate); //System.out.println(studentList.size()); StringBuffer sbHtml = new StringBuffer(); sbHtml.append(&amp;quot;&amp;lt;html&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;head&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;title&amp;gt;Student Search&amp;lt;/title&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;style type=\&amp;quot;text/css\&amp;quot;&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;table{&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;border:1px solid black;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot; border-collapse:collapse;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;}&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;td {&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;border:1px solid black;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;border-collapse:collapse;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;}&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;/style&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;/head&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;body&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;h1&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;出生日期:&amp;quot; + sBeginDate + &amp;quot; 至 &amp;quot; + sEndDate + &amp;quot; 学生列表&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;/h1&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;hr&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;table border=1&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;tr&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;学生代码&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;姓名&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;性别&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;出生日期&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;联系电话&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;家庭住址&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;班级名称&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;年龄&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;/tr&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;\n&amp;quot;); for (Iterator&amp;lt;Student&amp;gt; iter=studentList.iterator(); iter.hasNext();) { Student student = iter.next(); sbHtml.append(&amp;quot;&amp;lt;tr&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); sbHtml.append(student.getStudentId()); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); sbHtml.append(student.getStudentName()); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); sbHtml.append(student.getSex()); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); sbHtml.append(new SimpleDateFormat(&amp;quot;yyyy-MM-dd&amp;quot;).format(student.getBirthday())); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); sbHtml.append(student.getContactTel()); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); sbHtml.append(student.getAddress()); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); sbHtml.append(student.getClasses().getClassesName()); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;td&amp;gt;&amp;quot;); long b = 1000L*60L*60L*24L*365L; long a = System.currentTimeMillis() - student.getBirthday().getTime(); sbHtml.append(a/b); sbHtml.append(&amp;quot;&amp;lt;/td&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;/tr&amp;gt;&amp;quot;); } sbHtml.append(&amp;quot;&amp;lt;/table&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;/body&amp;gt;&amp;quot;); sbHtml.append(&amp;quot;&amp;lt;/html&amp;gt;&amp;quot;); response.setContentType(&amp;quot;text/html;charset=GBK&amp;quot;); response.getWriter().println(sbHtml.toString()); } } 

TestLifeCycleServlet.java

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

public class TestLifeCycleServlet extends HttpServlet {

	public TestLifeCycleServlet() {
		System.out.println(&amp;quot;--------TestLifeCycleServlet()----------&amp;quot;);
	}

	public void init()
          throws ServletException {
    	System.out.println(&amp;quot;-----------init-------------&amp;quot;);
    }

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		System.out.println(&amp;quot;------------doGet-------------&amp;quot;);
	}

	public void destroy() {

	}
}

UrlRewriteSession.java

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

public class UrlRewriteSession extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		response.getWriter().println(&amp;quot;&amp;lt;a href='&amp;quot; + response.encodeURL(request.getRequestURL().toString()) + &amp;quot;'&amp;gt;UrlRewrite&amp;lt;/a&amp;gt;&amp;quot;);
	}
}
Advertisements

About daviewning

I am an Oracle DBA

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: