java servlet 程式碼樣例 (demo)

流水無情88發表於2013-09-18

今天又搞了下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的朋友們好運。

相關文章