现在说明一下结局方法。
要提交的jsp页面addmodel.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mobile.service.convert" %>
<%
request.setCharacterEncoding("gb2312");
%>
<html>
<jsp:useBean id="db" scope="page" class="com.mobile.conn.mysql"/>
<head>
<title>add model</title>
<link rel="stylesheet" type="text/css" href="../css/style1.css"/>
<script type="text/javascript" src="../js/calendar.js"></script>
<script language="javascript">
var CalendarWebControl = new atCalendarControl();
function changepic(){
var fileName=document.all.file.value;
if(fileName=="")
document.all.pic.src="../img/product/nopic.jpg";
var exName=fileName.substring(fileName.lastIndexOf(".")+1).toUpperCase()
if(exName=="JPG"||exName=="GIF")
document.all.pic.src=document.all.file.value;
else{
alert("清选择图片格式为gif,jpg");
document.all.pic.src="../img/product/nopic.jpg";
}
}
</script>
</head>
<body>
<form name="form1" action="../domodel" method="POST" ENCTYPE="multipart/form-data">
<input type="hidden" name="kind" value="1">
<div align="center">
<center>
<table border="0" width="519" cellspacing="0" cellpadding="0" height="243">
<tr>
<td width="517" height="38" align="center" bgcolor="#FFFFFF" valign="top"><font size="5" color="#FFCC66">添加手机</font>
</td>
</tr>
<tr>
<td width="517" height="253">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="174">
<tr>
<td width="34%" height="41" align="right">*手机型号:</td>
<td width="136%" height="41" align="left" valign="middle"><input type="text" name="modelname" size="20" class="txt">
</tr>
<tr>
<td width="34%" height="44" align="right">*所属品牌:</td>
<%
String sql="select * from label order by idno desc";
ResultSet rs=db.executeQuery(sql);
%>
<td width="136%" height="44" align="left"><select size="1" name="labidno">
<%while(rs.next()){%>
<option value="<%=rs.getInt("idno")%>"><%=convert.getInstance().iso2gb(rs.getString("cname")) %></option>
<%} %>
</select></td>
</tr>
<tr>
<td width="34%" height="43" align="right">手机类型:</td>
<td width="136%" height="43" align="left"><select size="1" name="modelkind">
<option selected value="直板">直板</option>
<option value="翻盖">翻盖</option>
<option value="滑盖">滑盖</option>
<option value="其它">其它</option>
</select></td>
</tr>
<tr>
<td width="34%" height="43" align="right">手机图片:</td>
<td width="136%" height="43" align="left"><input name="file" type="file" onChange="changepic()" class="txt"><br>
<img src="../img/product/nopic.jpg" name="pic" width="100" height="200"></td>
</tr>
<tr>
<td width="34%" height="43" align="right">*发布时间:</td>
<td width="136%" height="43" align="left"><input type="text" name="pubtime" size="20" class="txt" readonly="true">
<img src="../img/btn_select_date_1.gif" width="18" height="18" onClick="CalendarWebControl.show(document.form1.pubtime,'',this)"></td>
</tr>
</table>
</td>
</tr>
<TR>
<td align="center">
<input type="submit" value="提交" class="btt"><input type="reset" value="重填" class="btt">
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
有点长看看我用颜色标记的地方就行。
下面是重点看看接收数据的servlet domodel.java
package com.mobile.service;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.io.*;
import javax.servlet.*;
import java.sql.*;
import com.mobile.conn.*;
import java.util.*;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.File;
public class domodel extends HttpServlet {
private mysql mysql=new mysql();
public String modelname;
public String modelimg;
public String modelkind;
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
PrintWriter out=response.getWriter();
SmartUpload smtup=new SmartUpload();
smtup.initialize(config,request,response);//初始化
try{
smtup.upload();
int kind=Integer.parseInt((smtup.getRequest()).getParameter("kind"));//这个就是接收了表单的kind数据
switch(kind){
case 1:
}
}
catch(Exception e){
out.print(e.toString());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
终结一下要点:1.表单的页面要将表单的属性即<form ENCTYPE="multipart/form-data">如上设置。表示表单上传的是多媒体数据
2.servlet的初始化实现config类。用做实现jspsmartupload类初始化的参数。用pagecontext也行。
3.实现JspSmartUpload类后要马上执行upload方法表示将表单所有数据接收过来
4.在第3步执行后使用(smtup.getRequest()).getParameter("kind")接受直接使用request对象不好用只能先使用getRequest()方法在进行接收
最后,感谢网上提供参考资料的作者们!http://www.webjx.com/htmldata/2005-08-19/1124406463.html非常感谢以上连接文章的作者,给我很大帮助!