演示录入图书信息的Servlet+jsp源代码(留着自己看看)

来源:互联网 发布:java三层架构怎么搭建 编辑:程序博客网 时间:2024/06/06 11:48

演示录入图书信息的Servlet+jsp源代码:


//过滤器:

package com.zpc;

 

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

 

 

public class CharactorFilter implementsFilter {

       Stringencoding=null;

   public CharactorFilter() {

 

    }

 

 

       publicvoid destroy() {

              encoding=null;

       }

 

// AddServlet 对象:

package com.zpc;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

 

 

public class AddServlet extends HttpServlet{

       privatestatic final long serialVersionUID = 1L;

 

   public AddServlet() {

       super();

      

    }

 

              protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

              doPost(request,response);

       }

 

      

       protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

              PrintWriterout=response.getWriter();

              Stringid=request.getParameter("id");

              Stringname=request.getParameter("name");

              Stringauthor=request.getParameter("author");

              Stringprice=request.getParameter("price");

              out.print("<h2>图书信息添加成功</h2><hr>");

              out.print("图书编号:"+id+"<br>");

              out.print("图书名称:"+name+"<br>");

              out.print("作者:"+author+"<br>");

              out.print("图书价格:"+price+"<br>");

              out.flush();

              out.close();

       }

 

}

 

 

 

//前台表单:

<%@ page language="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPEhtml PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert titlehere</title>

</head>

<body>

   <formaction="AddServlet"method="post">

      <tablealign="center"border="1"width="400">

      <tr>

        <tdclass="2"align="center"colspan="2">

        <h2>添加图书信息</h2>

        </td>

      </tr>

      <tr>

        <tdalign="right"> 图书编号:</td>

        <td>

           <inputtype="text"name="id">

        </td>

      </tr>

 

           <tr>

        <tdalign="right"> 图书名称:</td>

        <td>

           <inputtype="text"name="name">

        </td>

      </tr>

           <tr>

        <tdalign="right"> 作者:</td>

        <td>

           <inputtype="text"name="author">

        </td>

      </tr>

           <tr>

        <tdalign="right"> 图书价格:</td>

        <td>

           <inputtype="text"name="price">

        </td>

      </tr>

           <tr>

        <tdclass="2"align="center"colspan="2">

        <inputtype="submit"value="">

        </td>

 

      </tr>

      </table>

  

   </form>

</body>

</html>

 

       publicvoid doFilter(ServletRequest request, ServletResponse response, FilterChainchain) throws IOException, ServletException {

              if(encoding!=null){

                     request.setCharacterEncoding(encoding);

                     response.setContentType("text/html;charast="+encoding);

                     response.setCharacterEncoding(encoding);

                    

              }

              chain.doFilter(request,response);

       }

 

      

       publicvoid init(FilterConfig fConfig) throws ServletException {

              encoding=fConfig.getInitParameter("encoding");

       }

 

}

原创粉丝点击