<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>表单</title> </head> <body> <form action="tijiao" method="post"> 添加商品信息: <br><br> 商品名称: <input type="text" id="name" name="name"> <br><br> 商品厂商: <input type="text" id="changs" name="changs"> <br><br> 使用寿命: <input type="text" id="shoum" name="shoum"> <br><br> 厂家地址: <input type="text" id="dizhi" name="dizhi"> <br><br> <input type="submit" value="提交"> </form> </body> </html>
index.html
package com.hanqi; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class tijiao */ public class tijiao extends HttpServlet { private static final long serialVersionUID = 1L; String str = ""; /** * @see HttpServlet#HttpServlet() */ public tijiao() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ public void init(ServletConfig config) throws ServletException { str = config.getInitParameter("type"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if(str.equals("1")) { response.setCharacterEncoding("GBK"); String strName =new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8"); String strChangs =new String(request.getParameter("changs").getBytes("ISO-8859-1"),"UTF-8"); String strShoum =new String(request.getParameter("shoum").getBytes("ISO-8859-1"),"UTF-8"); String strDizhi =new String(request.getParameter("dizhi").getBytes("ISO-8859-1"),"UTF-8"); if(strName == null || strName.trim().length() ==0) { response.getWriter().append("商品名称不能为空"); } else if(strChangs == null || strChangs.trim().length() ==0) { response.getWriter().append("商品厂商不能为空"); } else if(strShoum == null || strShoum.trim().length() ==0) { response.getWriter().append("使用寿命不能为空"); } else if(strDizhi == null || strDizhi.trim().length() ==0) { response.getWriter().append("厂家地址不能为空"); } else { response.getWriter().append("商品名称:" + strName ); response.getWriter().append("<br>商品厂商:" +strChangs); response.getWriter().append("<br>使用寿命:" +strShoum); response.getWriter().append("<br>厂家地址:" +strDizhi); } //response.getWriter().append("Served at: asd ").append(request.getContextPath()).append(str); } else { response.getWriter().append("Served at:123 ").append(str); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
tijiao.java
时间: 2024-10-27 19:18:01