码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb学习总结-03 Servlet 学习和使用

时间:2016-08-20 11:24:59      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

一 Servlet

1 JSP与Servlet的生成方式

  Servlet首先被编译成class文件,然后被服务器调用

  JSP首先被转换为Servlet(java文件),然后在被编译为class文件,最后由服务器调用。

查看%/tomcat8\work\Catalina\localhost下项目中的jsp对应的Servlet的 java,class文件。

二 例子

1 第一个Servlet

新建java web项目 javaWeb1.

Servlet1.java

package com.test;

import java.io.IOException;
import java.io.PrintWriter;

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

public class Servlet1 extends HttpServlet {

    private static final long serialVersionUID = 1L;

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

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html>  ");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the <h1>GET method</h1>");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

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

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>javaWeb1</display-name>

    <servlet>
        <servlet-name>Servlet1</servlet-name>
        <servlet-class>com.test.Servlet1</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Servlet1</servlet-name>
        <url-pattern>/servlet/Servlet1</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 访问url地址 http://127.0.0.1:8080/javaWeb1/servlet/Servlet1,测试在 get,post下的返回值。

2 通过表单提交数据,servlet响应请求

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE  >
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>title1</title>
    <meta charset="utf-8">
  </head>
  
  <body>
  <form action="/javaWeb1/servlet/Servlet2">
      用户名: <input type="text" name="username"  maxlength="5" /> <br>
      密码: <input type="password" name="password" /> <br>
   <input type="submit" value="提交">&nbsp;&nbsp;
   <input type="reset" value="重置" >
  </form>
  </body>
</html>

  表单默认不设置提交方法是按照 get方式提交的, method可以设置get,post方式提交数据。

get和post方式的区别?

1)处理方式不同

2)地址栏呈现的结果不同

  get方式是把传递的参数放在url里。

  post方式是放在请求头里,地址栏里不会带参数。

 

Servlet2.java

package com.test;

import java.io.IOException;
import java.io.PrintWriter;

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

public class Servlet2 extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.process(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.process(request, response);
    }

    public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username=" + username);
        System.out.println("password=" + password);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>qqqqqq</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("username=" + username + "<br>");
        out.print("password=" + password);
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

}

 访问url地址 http://127.0.0.1:8080/javaWeb1/index.jsp

 3 通过表单提交数据,jsp响应请求

getPara.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username=" + username);
    System.out.println("password=" + password);
    
%>

<!DOCTYPE HTML >
<html>
<head>
<base href="<%=basePath%>">

<title> </title>
</head>

<body>

username=<%= username %> <br>
password=<%= password %>

</body>
</html>

index2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE  >
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>title1</title>
    <meta charset="utf-8">
  </head>
  
  <body>
  <form action="/javaWeb1/getPara.jsp" method="post"  >
      用户名: <input type="text" name="username"  maxlength="5" /> <br>
      密码: <input type="password" name="password" /> <br>
   <input type="submit" value="提交">&nbsp;&nbsp;
   <input type="reset" value="重置" >
  </form>
  </body>
</html>

 访问url地址 http://127.0.0.1:8080/javaWeb1/index2.jsp

 

JavaWeb学习总结-03 Servlet 学习和使用

标签:

原文地址:http://www.cnblogs.com/wangshuo1/p/javaee_3.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!