day02 Servlet

1 Servlet使用方法

package com.yuxhu.servlet;

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;

/* 1.创建一个类去继承HttpServlet
 * 2.重写doGet/doPost头方法
 *
 * HttpServletRequest  : 用来接受数据 (读)
 * HttpServletResponse : 用来响应数据 (写)
 * 
 * 直接运行是不行的,需要配置访问路径
 * 1.注解
 * 2.web.xml文件配置
 * 3.启动服务器之后页面怎么访问?
 *   localhost:8080/项目/访问路径(唯一)
 * 
 */
// @WebServlet(value="/yxh") //由于设置了servlet类,浏览器默认指向这里,所以不需要注解
public class ServletXml extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收请求 HttpServletRequest
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String user = new String(username.getBytes("iso-8859-1"),"utf-8");//解决乱码
        String pwd = new String(password.getBytes("iso-8859-1"),"utf-8");//解决乱码
                
        //响应结果 HttpServletResponse
        resp.setContentType("text/html;charset=utf-8"); //解决乱码
        PrintWriter pw = resp.getWriter();
        pw.write("账号:" + user + "<br>");
        pw.write("密码:" + pwd);
        
        //控制台
        System.out.println("账号:" + user);
        System.out.println("密码:" + pwd);
    }
}

2 设置 servlet 类 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Servlet1</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置 servlet类-->
  <servlet>
    <!-- servlet-name 名字随便取 -->
    <servlet-name>xml</servlet-name>
    <!-- 全类名 : 包名+类名 -->
    <servlet-class>com.ithc.demo.ServletXml</servlet-class>
  </servlet>
  <!--配置映射  -->
  <servlet-mapping>
    <!-- 需要与上面的name值一样 -->
    <servlet-name>xml</servlet-name>
    <!-- 配置访问路径 ,整个项目里面要唯一 -->
    <url-pattern>/xml</url-pattern>
  </servlet-mapping>

</web-app>

3 浏览器页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- 
        注释快捷键 : ctrl + shift + ?
        action : 访问的路径
        method : 访问的方法
        //以下语句注释也会打印出来
        ${pageContext.request.contextPath}//引用名字 可替换下面的xml
     -->
//form表单
     <form action="xml" method="post">
        <input type="text" name="username" placeholder="用户名">
        <br/>
        <input type="password" name="password" placeholder="密码">
        <br/>
        <input type="submit" value="确定">
     </form>

</body>
</html>

4 单选,多选,超链接

表单

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <form action="demo03" method>
    <!-- 多选框 -->
    <input type="checkbox" name="shop" value="mi">小米
    <br>
    <input type="checkbox" name="shop" value="huawei" checked="checked">华为
    <br>
    <input type="checkbox" name="shop" value="iphone">苹果
    <br>
    <!-- 单选框 -->
    <input type="radio" name="gender" value="男" checked="checked">男
    <input type="radio" name="gender" value="女">女
    
    <input type="submit" name="确定">
    
    </form>
    
    <!-- 超链接 -->
    <a href="demo03?gender=男">点击</a>

</body>
</html>

servlet

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <form action="demo03" method>
    <!-- 多选框 -->
    <input type="checkbox" name="shop" value="mi">小米
    <br>
    <input type="checkbox" name="shop" value="huawei" checked="checked">华为
    <br>
    <input type="checkbox" name="shop" value="iphone">苹果
    <br>
    <!-- 单选框 -->
    <input type="radio" name="gender" value="男" checked="checked">男
    <input type="radio" name="gender" value="女">女
    
    <input type="submit" name="确定">
    
    </form>
    
    <!-- 超链接 -->
    <a href="demo03?gender=男">点击</a>

</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,372评论 11 349
  • 走路要挺胸 收腹 准备用2017年一年的时间减脂,就像ADRIANNE HO那样。 饮食,训练,习惯。 10,3到...
    ZICOLOR阅读 230评论 0 0