java servlet 代码样例 (demo)

今天又搞了下jsp +servlet 的代码样例,感觉虽然搭了好多次,可是每次还是不记得那些参数,都要去网上搜索,索性自己把这次的简单demo给记录下来,供下次使用的时候直接复制吧。

这个web逻辑 input.jsp ---->ReportServlet -->output.jsp (在servlet中输入值,将参数传递到output.jsp中)

整个文档目录结构如下:

相关代码如下:

input.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="report" >
	<p> seller_id</p> 
	<input type="text" name="seller_id"></input>
	<label for="from_date">from_date:</label>
    <input type="text" id="from_date" name="from_date" />
    <label for="end_date">end_date:</label>
    <input type="text" id="end_date" name="end_date" />
    <input type="submit" id="submit" name="submit" />
</form>
</body>
</html>

 output.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
    List<String> result =(List)request.getAttribute("result");
%>
    
 <table>
 <% 
     for(String str:result)
    {
%>
<tr>
    <td>
    <%=str+"\n" %>
    </td>
</tr>
<%} %>
 </table>
</body>
</html>

 

web.xml(主要是注意servlet配置)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Report</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
        <servlet-name>reportServlet</servlet-name>
        <servlet-class>cn.edu.ReportServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>reportServlet</servlet-name>
        <url-pattern>/report</url-pattern>
    </servlet-mapping>
    
</web-app>

ReportServlet.java 内容

package cn.edu;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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


public class ReportServlet 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 from_date = request.getParameter("from_date");
            System.out.println(from_date);
            
            List <String> result = new ArrayList<String>();
            result.add("apple");
            result.add("apple");
            result.add("apple");
            result.add("apple");
            //HttpSession session=request.getSession();
            request.setAttribute("result", result);
            request.getRequestDispatcher("output.jsp").forward(request,response);
            //return ;
            //response.sendRedirect("/Report/output.jsp");
            //request.
            //response.sendRedirect(arg0)
            
//        response.setContentType("text/html;charset=utf-8");  
//        PrintWriter out = response.getWriter();  
//        request.setCharacterEncoding("utf-8");  
//        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
//        out.println("<HTML>");  
//        out.println("  <HEAD><TITLE>display survey information</TITLE></HEAD>");  
//        out.println("  <BODY>");  
//        out.println("<h2>用户输入的信息</h2>");  
//        out.println("用户名:"+request.getParameter("name")+"<br>");  
//        out.println("e-mail:"+request.getParameter("email")+"<br>");  
//        out.println("年纪:"+request.getParameter("age")+"<br>");  
//        out.println("编程时间:"+request.getParameter("codetime")+"<br>");  
//        out.println("你使用的操作系统:");  
//        printValues(out,request.getParameterValues("os"));  
//        out.println("你使用的编程语言:");  
//        printValues(out,request.getParameterValues("language"));  
//        out.println("建议:"+request.getParameter("comment")+"<br>");  
//        out.println("  </BODY>");  
//        out.println("</HTML>");  
//        out.flush();  
//        out.close();  
    }  
      
    public void printValues(PrintWriter out,String[] values){  
        if(values==null || values.length==0)  
            return;  
        out.println("<ul>");  
        for(int i=0;i<values.length;i++){  
            out.println("<li>"+values[i]+"</li>");  
        }  
        out.println("</ul>");  
    } 
}
View Code

 

好了,就这样了,祝学习web的朋友们好运。

posted @ 2013-09-18 16:39  流水无情88  阅读(3457)  评论(0编辑  收藏  举报