关于Servlet的基本使用方法

一、第一个Servlet
1、解压空站点,运行:http://localhost:8088/myweb/
2、编写Java文件:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
        public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body bgcolor=/"white/">");
        out.println("<head>");
        out.println("<title>Hello!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("Hello World!");
        out.println("</body>");
        out.println("</html>");
    }
}

编译时需要<Tomcat>/common/lib下的包servlet-api.jar,如:
javac -classpath "C:/Program Files/Apache Software Foundation/Tomcat 5.0/common/lib/servlet-api.jar" HelloWorld.java

3、将class文件放入站点的WEB-INF/classes目录

4、修改web.xml文件(区分URL的大小写):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
        <display-name></display-name>
        <description></description>
        <servlet>
                <servlet-name>HelloWorld</servlet-name>
                <servlet-class>HelloWorld</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>HelloWorld</servlet-name>
                <url-pattern>/hello</url-pattern>
        </servlet-mapping>
</web-app>

5、运行:
http://localhost:8088/myweb/hello

(可能要重启tomcat)

说明:关于jar文件的存放
server/lib下的jar只能被tomcat访问使用
shared/lib下的jar只能被所有web应用程序访问使用
common/lib下的jar即可以被tomcat访问使用,也可以被所有web应用程序访问使用
应用程序的WEB-INF/lib下的jar只能被当前应用程序访问使用

6、问题:
1)修改web.xml文件为:
<servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>

2)放入jsp-examples应用程序中运行之

二、使用Eclipse开发

三、基本使用
1、中文问题,修改doGet方法,如:
                response.setContentType("text/html;charset=GBK");
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<body bgcolor=/"white/">");
                out.println("<head>");
                out.println("<title>你好!</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("欢迎来到Servlet!");
                out.println("</body>");
                out.println("</html>");

2、Post和Get提交
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
        public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>Post</body></html>");
    }
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>Get</body></html>");
    }
}

直接通过浏览器键入:http://localhost:8088/lsq/HelloWorld
发现显示为:Get

但是通过下面的网页访问此servlet,却显示为:Post
<html>
<head>
</head>
<body>
<form method="POST" action="http://localhost:8088/lsq/HelloWorld">
  <input type="submit" value="提交" name="B1">
</form>
</body>
</html>

更有意思的组合:
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException {
                doGet(request, response);
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException {
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.println("<html><body>Get</body></html>");
        }

3、更加灵活的字符串输出
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        StringBuffer buf = new StringBuffer();
        buf.append("<html><body>Get</body></html>");
        out.println( buf.toString() );
        out.close();
    }
}

4、显示时间
代码为:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;

public class HelloWorld extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Date d=new Date();      
        StringBuffer buf = new StringBuffer();
        buf.append(d.toString());
        out.println( buf.toString() );
        out.close();
    }
}

5、参数的使用
参数分为post参数和get参数两种,都可以使用HttpServletRequest对象的getParameter()获取,此方法的参数即为servlet参数的名称,如:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
    {
        String value=request.getParameter("postKey");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        StringBuffer buf=new StringBuffer();
        buf.append("<html><body>");
        buf.append(value);
        buf.append("</body></html>");
        out.println(buf.toString());
        out.close();
   }
  
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
    {
        String value=request.getParameter("getKey");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        StringBuffer buf=new StringBuffer();
        buf.append("<html><body>");
        buf.append(value);
        buf.append("</body></html>");
        out.println(buf.toString());
        out.close();
   }
}

对于Post方式,访问网页为:
<HTML>
   <HEAD>
   </HEAD>
   <BODY>
      <FORM METHOD="POST" ACTION="http://localhost:8088/myweb/hello">
        <input type="text" name="postKey" size="20"><INPUT TYPE=submit VALUE="提交">
      </FORM>
   </BODY>
</HTML>

对于get方式,访问方式为:
http://localhost:8088/myweb/hello?getKey=5656

注意汉字参数的处理:
        String value=request.getParameter("getKey");
        value = new String(value.getBytes("ISO-8859-1"), "GB2312");
        response.setContentType("text/html;charset=GB2312");

6、使用集合遍历的方式得到所有的参数,如:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException {
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.println(request.getQueryString() + "<p>");
                Enumeration enumParam = request.getParameterNames();
                while (enumParam.hasMoreElements()) {
                        String paramName = (String) enumParam.nextElement();
                        String paramValues[] = request.getParameterValues(paramName);
                        if (paramValues != null)
                                for (int i = 0; i < paramValues.length; i++)
                                        out.println(paramName + " (" + i + "): " + paramValues[ i ]
                                                        + "<p>");
                }
        }
}

直接运行http://localhost:8088/myweb/hello
得到结果
null

运行http://localhost:8088/myweb/hello?f=a&g=d
得到结果
f=a&g=d
g (0): d
f (0): a

运行http://localhost:8088/myweb/hello?f=a&g=d&f=g
得到结果
f=a&g=d&f=g
g (0): d
f (0): a
f (1): g

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值