jsp,servlet类画圆的实现,题目如下:

12.编写inputCircle. jsp,页面提供form表单,该form表单提供两个text 文本框,用用户输人圆的圆心(例如(12,34))和圆的半径,用户单击submit提交键请求名drawCircle的servlet.编写创建servlet的Servler类,该类创建的servlet可以绘制圆。

一、使用form表单实现inputCircle的jsp页面:

inputCircle. 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=utf-8">
<title>inputCircle</title>
</head>
<body>
<form action="drawCircle" method="post" id=textStyle>
请输入圆心位置:
<input type="text" id=textStyle name="yuanxing" value="(250,250)" >
(格式用xx,xx)
<br>请输入圆的半径:
<input type="text" id=textStyle name="banjing" value="200">
<br>
<input type="submit" id=textStyle value="提交">

</form>
</body>
</html>

1.提交的名称写类的名字,提交方式使用get和post都行

<form action="drawCircle" method="post" id=textStyle>

2.记住自己取的名字,后面用的上,并且设置初始值,使用的时候就不用再打

<input type="text" id=textStyle name="yuanxing" value="(250,250)" >   
(格式用xx,xx)
<br>请输入圆的半径:
<input type="text" id=textStyle name="banjing" value="200">
<br>
<input type="submit" id=textStyle value="提交">

二、在Java Resource下的src中创建包 名为:moon.sun;包下创建drawCircle的servlet类 

 

 drawCircle.java

package moon.sun;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class drawCircle
 */
@WebServlet("/drawCircle")
public class drawCircle extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public drawCircle() {
        super();
        // TODO Auto-generated constructor stub
    }
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/jpeg");
    	String yuanxing=request.getParameter("yuanxing");
    	String banjing=request.getParameter("banjing");
    	if(yuanxing == null||yuanxing.length()==0 ||banjing == null||banjing.length()==0){
            response.sendRedirect("inputCircle.jsp");//重定向到输入数据页面。
            return;
        }  
    	
    	 int i=0;
    	 Pattern pattern;          //模式对象。
         Matcher matcher;          //匹配对象。
         String regex="-?[0-9][0-9]*[.]?[0-9]*" ;//匹配整数的正则表达式。
         pattern = Pattern.compile(regex);  //初试化模式对象。
         matcher = pattern.matcher(yuanxing);  //初始化匹配对象,用于检索input。
         String[] Number=new String[2];
         while(matcher.find()) {
             String str = matcher.group(); 
             Number[i]=str;
             i++;  
          } 
    	double x=Double.parseDouble(Number[0]);
    	double y=Double.parseDouble(Number[1]);
    	Double r=Double.parseDouble(banjing);
    	draw(r,x,y,response);
	}
    		
	void draw(double r,double x,double y,HttpServletResponse response){
		 int width=800, height=600;
	        BufferedImage image = 
	        new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	        Graphics g = image.getGraphics();
	        g.fillRect(0, 0, width, height);
	        Graphics2D g_2d=(Graphics2D)g; 
	        Ellipse2D ellipse=new  Ellipse2D.Double(x-r,y-r,2*r,2*r);
	        g_2d.setColor(Color.blue);
	        g_2d.draw(ellipse); 
	        try{
	        	OutputStream outClient = response.getOutputStream();
	        	boolean boo =ImageIO.write(image, "jpeg", outClient);
	        }
	        	catch(Exception exp){}	        
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}

1.直接在eclipse新建servlet类,勾选doget与dopost方法,方法写在get与post里面都行因为他们互相调用

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

 2.代码大概可以分为三部分一是初始化对象,与jsp页面建立联系;二是使用正则表达式与数组将圆心位置赋予给x,y;三就是画圆方法

初始化对象,判断是否为空

response.setContentType("image/jpeg");
    	String yuanxing=request.getParameter("yuanxing");
    	String banjing=request.getParameter("banjing");
    	if(yuanxing == null||yuanxing.length()==0 ||banjing == null||banjing.length()==0){
            response.sendRedirect("inputCircle.jsp");//重定向到输入数据页面。
            return;
        }  

 赋予给x,y

int i=0;
    	 Pattern pattern;          //模式对象。
         Matcher matcher;          //匹配对象。
         String regex="-?[0-9][0-9]*[.]?[0-9]*" ;//匹配整数的正则表达式。
         pattern = Pattern.compile(regex);  //初试化模式对象。
         matcher = pattern.matcher(yuanxing);  //初始化匹配对象,用于检索input。
         String[] Number=new String[2];
         while(matcher.find()) {
             String str = matcher.group(); 
             Number[i]=str;
             i++;  
          } 
    	double x=Double.parseDouble(Number[0]);
    	double y=Double.parseDouble(Number[1]);
    	Double r=Double.parseDouble(banjing);
    	draw(r,x,y,response);

画圆的方法与dopost方法是同等的,它独立出来

void draw(double r,double x,double y,HttpServletResponse response){
		 int width=800, height=600;
	        BufferedImage image = 
	        new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	        Graphics g = image.getGraphics();
	        g.fillRect(0, 0, width, height);
	        Graphics2D g_2d=(Graphics2D)g; 
	        Ellipse2D ellipse=new  Ellipse2D.Double(x-r,y-r,2*r,2*r);
	        g_2d.setColor(Color.blue);
	        g_2d.draw(ellipse); 
	        try{
	        	OutputStream outClient = response.getOutputStream();
	        	boolean boo =ImageIO.write(image, "jpeg", outClient);
	        }
	        	catch(Exception exp){}	        
	}

实现的效果图

 

 

到这里就结束了,有问题可以在评论区问我,但我不一定会,大家一起讨论学习,如果这篇对你用,请点个赞,谢谢!

  • 32
    点赞
  • 109
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
甘肃政法学院 本科生实验报告 (一) 姓名: 学院:计算机科学学院 专业: 计算机科学与技术 班级 实验课程名称: 实验日期:2012 年 04 月 9 日 指导教师及职称 实验成绩: 开课时间:2012 学年 二 学期 甘肃政法学院实验管理中心印制 实验题目 Java Web 项目开发环境搭建于简单开发 Jsp 指令标记与动作标记 Jsp 内置对象 小组合作 否 姓名 班级 学 号 一、实验目的 1、 熟悉 Java Web 开发环境 JDK1.6+Tomcat6.0+MyEclipse8.5 开发环境的搭建方法。 2、 能够从实际问题出发,编写出简单的 JSP 程序,并将其正确发布和测试。 3、熟练掌握怎样在 JSP 页面中使用 page 指令设置 contentType 的值。 4、使用 include 指令在 JSP 页面中静态插入一个文件的内容。 5、掌握怎样在 JSP 页面中使用 include 标记动态加载文件。 6、使用 forward 实现页面的转向。 7、熟练掌握怎样在 JSP 页面中使用 request 内置对象。 8、熟练掌握怎样在 JSP 页面中使用 response 对象动态响应用户的请求。 8、熟练掌握怎样在 JSP 页面中使用 session 对象存储和用户有关的数据。 9、进一步熟悉其它 JSP 内置对象的用法。 二.实验环境 装有 Myeclipse 8.5,Tomcat6.0 的计算机一台 三、实验内容与步骤 《一》简单 Java Web 项目的开发与环境搭建 1.JDK1.6,Tomcat6.0,MyEclipse8.5 的安装和配置。 2. 编写一个求解 1—1000 内是"完数"的正整数的 JSP 代码,要求将其发布在 Tomcat 服务器中 3. 试在 Myeclipse 环境下重复开发前一个项目。 4. 编写两个 JSP 页面,名字分别为 inputName 和 people.jsp。 (1)inputName.jsp 的具体要求 该页面有一个表单,用户通过该表单输入自己的姓名并提交给 people.jsp 页面。 (2)people.jsp 的具体要求 JSP 页面有名字为 person、型是 StringBuffer 以及名字是 count,型为 int 的成 员变量。 JSP 有 public void judge ()方法。 该方法负责创建 person 对象, 当 count 的值是 0 时, judge ()方法创建 person 对象。 JSP 有 public void addPerson(String p)的方法,该方法将参数 p 指定的字符串尾加 到操作成员变量 person,同时将 count 作自增运算。 JSP 页面在程序片中获取 inputName.jsp 页面提交的姓名,然后调用 judge ()创建 person 对象、调用 addPerson 方法将用户的姓名尾加到成员变量 person。 如果 inputName.jsp 页面没有提交姓名,或姓名含有的字符个数大于 10,就使用 <jsp:forward page="要转向的页面" />标记将将用户转到 inputName.jsp 页面。 通过 Java 表达式输出 person 和 count 的值。 《二》Jsp 指令标记与动作标记 1. 编写三个 JSP 页面:first.jsp 、second.jsp 和 third.jsp。另外,要求用"记事本"编 写一个 txt 文件 hello.txt。hello.txt 的每行有若干个英文单词,单词之间用空格分隔,每行之 间用"<BR>"分隔, first.jsp 的具体要求 first.jsp 使用 page 指令设置 contentType 属性的值是"text/plain",使用 include 指令静态 插入 hello.txt 文件。 second.jsp 的具体要求 second.sp 使用 page 指令设置 contentType 属性的值是"application/vnd.ms-powerpoint", 使用 include 指令静态插入 hello.txt 文件。 third.jsp 的具体要求 third.jsp 使用 page 指令设置 contentType 属性的值是"application/msword", 使用 include 指令静态插入 hello.txt 文件。 2. 编写四个 JSP 页面:one.jsp 、two.jsp 和 three.jsp 和 error.jsp。one.jsp 、two.jsp 和 three.jsp 页面都含有一个一个导航条,以便让用户方便地单击超链接访问这三个页面,要求 这

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉游星河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值