2021-07-15

本文概述了JSP项目中前台与后台通信的关键,包括get和post方法的区别,以及如何使用session进行数据传递。特别介绍了SmartUpload图片上传的实现步骤,展示了如何利用SmartUpload封装上传请求并处理文件操作。
摘要由CSDN通过智能技术生成

网络编程总结之JSP项目中,前台与后台如何进行通信?前台与后台如何进行数据交互?

JSP项目相关技术已经十分成熟,那么JSP项目其中的关键要点在哪里呢?

1.前台与后台如何通信?
要想将前台的数据发送到后台,主要有两种方式,一种是get方法,一种是post方法。
get方法与post方法在本质上是一致的,他们都是http协议中发送请求的方法。http是基于TCP/IP协议如何在万维网中进行通信的协议,也就意味着,get方法与post方法的底层也是TCP/IP协议。

get方法与post方法的区别:

1.get方法从服务器中获取数据,而post方法向服务器发送数据。
2.get方法传送的数据较少,小于2kb,而post方法发送数据的体量较大。
3.get方法将相关信息置于URL中,而post方法将数据存于请求主体中。

要想将后台的数据发送到前端供前台使用,个人推荐使用session。session对象存储特定用户会话所需要的属性及基本特性。其基本的使用方法如下:

request.getSession().setAttribute("AddrAddSuccess", "地址添加成功!");

其中第一个参数是session中的参数名,十分重要,不能重复,否则会产生覆盖,无法获取到指定信息。第二个参数是该session下属性对应的内容,数据类型不限。

那么在后台中如何获取session中的属性呢?代码如下:

request.getSession().getAttribute("属性名");

在前端中可以使用jstl标签库来进行session中信息的读取与逻辑判断

2.smartupload图片与视频文件如何上传?

smartupload将post请求主体封装成二进制比特流文件,其代码如下:
前端代码:

 <form action="/OnlineMarket_V2/manage/DoCmdAdd" method="post" id="myform" name="myform" enctype="multipart/form-data">
                    <table class="insert-tab" width="100%">
                        <tbody>   
                        <tr>
                     
                            <th width="120"><i class="require-red">*</i>分类:</th>
                            <td>
                            
                                <select name="CCate" id="catid" class="required">
                                    <option value="">请选择</option>
                                 
                                     <c:forEach var="c" items="${catelist }"> 
                                     <option value="${c.id }">${c.cateName }</option> 
                                     </c:forEach>                                
       
                                </select>
                               
                            </td>
                        </tr>
                       
                            <tr>
                                <th><i class="require-red">*</i>商品名称:</th>
                                <td>
                                    <input class="common-text required" id="title" name="CName" size="50" value="" type="text">
                                </td>
                            </tr>
                            <tr>
                            <c>
                                <th>上传者:${user.userName }</th>
                                <td><input class="common-text" name="author" size="50" value="admin" type="text"></td>
                           </c>
                            </tr>
                            <tr>
                                <th><i class="require-red">*</i>商品价格:</th>
                                <td>
                                    <input class="common-text required" id="title" name="CPrice" size="50" value="" type="text">
                                </td>
                            </tr>
                            <tr>
                                <th>商品数量:</th>
                                <td><input class="common-text" name="CCount" size="50" value="" type="text"></td>
                            </tr>
                            <tr>
                                <th><i class="require-red">*</i>缩略图:</th>
                                <td><input name="smallimg" id="" name="photo" type="file"><!--<input type="submit" οnclick="submitForm('/jscss/admin/design/upload')" value="上传图片"/>--></td>
                            </tr>
                            <tr>
                                <th>详细描述:</th>
                                <td><textarea name="CDesc" class="common-textarea" id="content" cols="30" style="width: 98%;" rows="10"></textarea></td>
                            </tr>
                            <tr>
                                <th></th>
                                <td>
                                    <input class="btn btn-primary btn6 mr10" value="提交" type="submit">
                                    <input class="btn btn6" onClick="history.go(-1)" value="返回" type="button">
                                </td>
                            </tr>
                        </tbody></table>
                </form>
            </div>
        </div>

    </div>

后台代码:

	request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
	    //定义对象
		SmartUpload  su=new SmartUpload();
		//初始化
	     su.initialize(this.getServletConfig(), request,response);
	     //设定允许上传的文件类型, 通过后缀名来进行限制
	     su.setAllowedFilesList("jpg,png,bmp,jpeg");
	     try {
	    	 //上传
			su.upload();
		} catch (ServletException | IOException | SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     
	     CateDao catedao=new CateDao();
	     Request suRequest=su.getRequest();
		 String Cname=suRequest.getParameter("CName");
		 String Ccount=suRequest.getParameter("CCount");
		 String Cprice=suRequest.getParameter("CPrice");   
		 String Cdesc=suRequest.getParameter("CDesc");
		 String CCate=suRequest.getParameter("CCate");
		 System.out.println(CCate);
	Integer CateId=Integer.parseInt(CCate);
		 
	     //生成三伪随机数v
	     int timestamp=(int)((Math.random()*9+1)*1000);
		 System.out.println(timestamp);   
		 
	     //获取上传的文件对象
	    Files files=su.getFiles();
	    //获取文件名后缀
	    String fileext=files.getFile(0).getFileExt();
	    //获取文件名前缀
	    String filefont=files.getFile(0).getFileName().substring(0,files.getFile(0).getFileName().indexOf("."));
	    String filename=Cname+filefont+'-'+timestamp+'.'+fileext;
	    try {
	files.getFile(0).saveAs("/images/product"+java.io.File.separator+filename);//重命名文件与存储路径,关键!,防止图片因为文件名相同而覆盖
			
		} catch (IOException | SmartUploadException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

持续更新,欢迎关注!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值