package com.print.biz;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.print.entity.Shape;
import com.print.entity.ShapeInfo;
public class PrintServlet extends HttpServlet {
/**
* 处理Get请求
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//如果图形信息为空,则转到图形选择页面
if(request.getParameter("shape") != null && request.getParameter("style") != null
&& request.getParameter("shape").trim() == "" && request.getParameter("style").trim() == ""
){
response.sendRedirect(request.getContextPath() + "/shapeInfo.jsp");
return;
}//获取表单信息
else if (request.getParameter("shape") != null && request.getParameter("style") != null &&
request.getParameter("shape").trim() != "" && request.getParameter("style").trim() != "")
{
ShapeInfo shapeInfo = new ShapeInfo(); //实例化图形类
int shapeId = Integer.parseInt(request.getParameter("shape"));
Shape shape = Shape.getShapeById(shapeId); //获取形状的枚举值
//将获取的值封装到ShapeInfo类中
shapeInfo.setShapeId(shape);
shapeInfo.setShapeStyle(new String(request.getParameter("style").getBytes("ISO-8859-1")));
request.setAttribute("shapeInfo", shapeInfo); //用request保存图形信息
request.getRequestDispatcher("printShape.jsp").forward(request, response); //转发到打印页面
}
}
/**
* 处理Post请求
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}