快捷搜索:

几个实用的Servlet应用例子-入门、cookie、session及

Servlet可以被觉得是办事真个applet,它被WEB办事器加载和履行,前端可以显示页面和得到页面数据,后台可以操纵数据库,能完成JavaBean的很多功能。在这里我较为具体的说说Servlet在Cookie,Session和上传文件上的利用,在阐明时我给出一些能编绎运行的小例子,着末给出一个文件上传例子以加深印象。

我们先来看看SERVLET法度榜样的基础构架:

式1:

package test;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class test extends HttpServlet {

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int f =1; switch(f){

case 1:firstMothed(request,respponse);break;

}

}

public void firstMothed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

OutputStreamWriter osw = new

OutputStreamWriter(response.getOutputStream());

PrintWriter out = new PrintWriter (response.getOutputStream());

out.println("");

out.println("Servlet1");

out.println("你好!");

out.println("");

out.close();

}

}

式2:

package test;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class test extends HttpServlet {

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());

PrintWriter out = new PrintWriter (response.getOutputStream());

out.println("");

out.println("Servlet1");

out.println("你好!");

out.println("");

out.close();

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());

PrintWriter out = new PrintWriter (response.getOutputStream());

out.println("");

out.println("Servlet1");

out.println("你好!");

out.println("");

out.close();

}

}

式1得当于作总控模块,此SERVLET作中心调整,根据不合的f值调用不合的SERVLET或措施。

式2得当于对html的get和post有不合要求的环境。

但这并不是绝对的,式2就完全可以代替式1,只要在doGet()措施中写上doPost就与式1完全一样。

在init措施中履行的语句,只要这个servlet被启动了就不停有效,比如,我们在init()中new了一个工具,那么这个工具的内存空间就永世存在,除非显式地把这个工具赋为null,或重启办事。

HttpServletRequest和HttpServletResponse两个工具实现http哀求,它们有很多有用的措施,鄙人面的cookie和session治理中会细加描述。

1, cookie治理 cookie用于在客户端保存小我所特有的信息,它采取在客户机写临时文件的机制。

package test;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class test extends HttpServlet {

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//写cookie

String CookieName ="js79"; //若是汉字则需编码

String CookieValue = "yesky";//若是汉字则需编码

Cookie cookie = new Cookie(CookieName,CookieValue);

cookie.setMaxAge(age); // age = Integer.MAX_VALUE 永不过时

cookie.setPath("/");

//读cookie

String value = null;

Cookie[] cookies = request.getCookies();

if (cookies != null) {

for (int i=0; i");

out.println("test");

out.println("cookie键:"+CookieName+"

");

out.println("cookie值: "+value);

out.println("");

out.close();

}

}

2,session治理

Session在Servlet中是很有用的,它比cookie安然靠得住机动,然则治理起来有点麻烦,用得不好会造成办事器的开销很大年夜,挥霍资本。下面是一个基于Session治理一个工具的简单例子。

一个简单的bean工具TestObject

package test;

public class TestObject extends Object {

int id = 0; public String cur="";

}

package test;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class TestMan extends HttpServlet {

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int f = 1;

if(request.getParameter("f")!=null)

f =

Integer.parseInt(request.getParameter("f"));

switch(f){

case 1: this.getResult(request,response);

break;

case 2:

this.setSession(request,response);

break;

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request,response);

}

public void getResult(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {

TestObject testObject = null;

testObject = getStatus(request,response);

String html = testObject.id; doWrite( response,html);

}

public void setSession(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

HttpSession session = request.getSession();

TestObject testObject = null;

testObject = getStatus(request,response);

String tmp = null;

tmp = request.getParameter("id");

if(tmp != null) testObject.id = tmp;

session.putValue("testObject ",article);

getResult(request,response);

}

private TestObject getStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession session = request.getSession();

TestObject testObject = null;

if(session!=null){

if(session.getValue("testObject ")!=null){

testObject = (TestObject)session.getValue("testObject ");

}

else{

testObject = new TestObject ();

}

}

else{

testObject = new TestObject ();

}

return testObject;

}

private void doWrite(HttpServletResponse response,String html) throws   ServletException, IOException {

PrintWriter out = response.getWriter();

out.println(html);

out.close();

}

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

}

若能轻松搞定上面的例子,信托读者对SERVLET已有了较为深刻的理解。

下面再先容一个上传文件例子,此中汲及到了下载的免费JavaBean (如有感兴趣的同伙,可来函索要免费JavaBean源代码,Email:js79@yesky.com)

上传基滥觞基本理:由页面发出一个http哀求,办事端获得哀求后,解析多媒体协议,读出文件内容,写文件内容到办事器,所有的这些功能都封装到JavaBean中。

上传文件的必需前提:Browser端表单的ENCTYPE属性值必须为 multipart/form-data,它奉告我们传输的数据要用到多媒体传输协议,因为多媒体传输的都是大年夜量的数据,以是规定上传文件必须是post措施,的type属性必须是file。

package upload;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class UpLoadServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException{

super.init(config);

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();

out.println("UpLoad"

+""

+""

+"");

out.println("

"

+"请你选择上传的文件(请留意文件大年夜小只能在20K之内)

"

+""

+""

+""

+""

+"");

out.println("");

out.close();

}

////////

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();

int tmpID = 1; try {

MultipartRequest multi = new MultipartRequest(request,"/home/js79/html/", 5 * 1024 * 1024);

}

catch(Exception e){

tmpID = -1; System.out.println(e);

}

if(tmpID == 1){

out.println("UpLoad"

+""

+""

+"");

out.println("上传成功!");

}

else{

out.println("UpLoad"

+""

+""

+"");

out.println("上传不成功!");

}

out.close();

}

}

您可能还会对下面的文章感兴趣: