当前位置:多学网学习教育电脑学习编程入门JSP教程jsp技巧大全

jsp技巧大全

[08-23 22:09:45]   来源:http://www.duoxue8.com  JSP教程   阅读:128
jsp技巧大全,标签:JSP编程技巧,jsp开发,jsp实用教程,jsp视频教程,http://www.duoxue8.com


transport.send(newMessage); 


将所有的组件结合在一起 


现在所有的组件都已经齐全了。现在将它们都放在JSP里面。要注意每一个错误信息,并将它反馈给用户。代码如下,你可以通过复制它们直接使用: 


Sample JSP email Utility Using JavaMail 

<%@ page 

import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" 

%> 

<html> 

<head> 

<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE> 

</HEAD> 

<BODY> 

<% 

try{ 

Properties props = new Properties(); 

Session sendMailSession; 

Store store; 

Transport transport; 

sendMailSession = Session.getInstance(props, null); 

props.put("mail.smtp.host", "smtp.jspinsider.com"); 

Message newMessage = new MimeMessage(sendMailSession); 

newMessage.setFrom(new InternetAddress(request.getParameter("from"))); 

newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to"))); 

newMessage.setSubject(request.getParameter("subject")); 

newMessage.setSentDate(new Date()); 

newMessage.setText(request.getParameter("text")); 

transport = sendMailSession.getTransport("smtp"); 

transport.send(newMessage); 

%> 

<P>Your mail has been sent.</P> 

<% 

catch(MessagingException m) 

out.println(m.toString()); 

%> 

</BODY> 

</HTML> 

你会很快体会到JavaMail的方便之处,JSP和JavaMail将是未来的希望。

文件/图片上传
package uploadfile; 

import javax.servlet.ServletInputStream; 
import javax.servlet.http.HttpServletRequest; 
import java.io.FileOutputStream; 
import java.io.*; 
import java.util.Hashtable; 
import java.util.*; 

public class FileUploadBean { 

private String savePath=null; //文件上传保存的路径 
private String contentType=""; //内容类型 
private String charEncode=null; //字符编码 
private String boundary=""; //分界线 
private String fileName=null; //本地文件名字 
private Hashtable dic=new Hashtable(); //用于保存"元素名--元素值"对 
private int totalSize=0; //上传文件总大小 
private String path=""; //保存文件的路径 
private String newFileName=""; //存入随机产生的文件名 


/////////////////////////////////////////////////// 
//设置文件上传保存的路径 
public void setSavePath(String s) { 
s=path+s; 
savePath=s; 
System.out.println("上传路径:"+savePath); 

/////////////////////////////////////////////////// 
//取文件上传保存的路径 
public String getSavePath() { 
return savePath; 

//////////////////////////////////////////////////// 
//设置文件名字,也可以为它命名,暂时先用它原来的名字 
public void setFileName(String s) { 
int pos=s.indexOf(""; filename=""); 
if (pos>0) { 
s=s.substring(pos+13,s.length()-3); //去 " 和 crlf 
pos=s.lastIndexOf("\"); 
if (pos<0) 
pos=s.lastIndexOf("/"); 
if (pos<0) 
fileName=s; 
fileName=s.substring(pos+1); 

//////////////////////////////////////////////////// 
//取得文件名 
public String getFileName() { 
System.out.println("得到文件名"+newFileName); 
return newFileName; 


/////////////////////////// 
//以时间为种子数产生新文件名 
public String getNewFileName() { 
int pos=0; //.的位置 
long seed=0; //随机种子数 
String ext=""; //存入文件扩展名 
System.out.println("upload file name:"+fileName); 
pos=fileName.lastIndexOf("."); 
ext=fileName.substring(pos); //得到扩展名 
seed=new Date().getTime(); 
Random rand=new Random(seed);//以时间为种子产生随机数作为文件名 
newFileName=Long.toString(Math.abs(rand.nextInt()))+ext; //生成文件名 
System.out.println("new file name:"+newFileName); 
return newFileName; 


////////////////////////////////////////////////////// 
//设置字符的编码方式 
public void setCharEncode(HttpServletRequest req) { 
charEncode=req.getCharacterEncoding(); 


///////////////////////////////////////////////// 
//设置得ContentType 
public void setBoundary(HttpServletRequest req) { 
//传递的参数值类似"multipart/form-data; boundary=---------------------------7d21441a30013c" 
//传过来的分界线比实际显示在上传数据中的要多两个"--" 

boundary=req.getContentType(); 
//System.out.println("boundary"+boundary); 
int pos=boundary.indexOf("boundary="); 
//加上这两个"--" 
boundary="--"+boundary.substring(pos+9); 


//////////////////////////////////////////////////// 
//取得ContentType 
public String getBoundary(){ 
//返回值类似"-----------------------------7d21441a30013c" 
return boundary; 


///////////////////////////////////////////// 
//设置ContentType 
public void setContentType(String s) { 
int pos =s.indexOf(": "); 
if (pos!=-1) 
contentType=s.substring(pos+2); 

//////////////////////////////////////////// 
//取得ContentType 
public String getContentType() { 
return contentType; 

///////////////////////////////////////////// 
//初始化 
public void init(HttpServletRequest req) { 
setCharEncode(req); 
setBoundary(req); 


//////////////////////////////////////////////////// 

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  下一页


jsp技巧大全 结束。
Tag:JSP教程JSP编程技巧,jsp开发,jsp实用教程,jsp视频教程电脑学习 - 编程入门 - JSP教程
jsp技巧大全相关文章