zoukankan      html  css  js  c++  java
  • servlet_1

    HttpServlet超类在java构建路径中没有找到
    https://jingyan.baidu.com/article/6c67b1d69a37a02787bb1ee2.html
    
    ===============================================
    servlet处理http请求1
    	客户端发送http请求的方式
    	HttpServletRequest接口概述
    	获取http请求参数的方法
    	
    	客户端发送http请求的方式
    		地址栏直接输入URL
    		超链接的href指定的URL
    		表单标签form的action指定的URL
    		javascript的location.href指定URL
    		
    	HttpServletRequest接口
    		继承自ServletRequest接口,处理http请求
    		封装了http请求的请求url,请求参数,请求报文等信息
    		由容器实现,开发中直接从doMethod的参数中获取其实例
    			
    	
    	获取http请求参数的方法
    		从以do开头的方法中直接获取。
    		
    servlet处理http请求2
    	
    	获取表单控件中的参数
    	
    	处理console乱码,临时地加入下面一行
    	request.setCharacterEncoding("utf-8");
    	空字符串	用户名与密码未填写时
    	null	单选框未选中时
    	空指针	复选框一个都不选时
    <%@ 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>
        <a href="Servlet1?name=tom&name=jack&gender=male">请求1</a>
        <form action="Servlet2" method="post">
            <input type="text" name="username"/><br /> 
            <input type="password" name="userpwd"/><br /> 
            <input type="radio" name="gender" value="f"  /><input type="radio" name="gender" value="m" /><br /> 
            <input type="checkbox" name="favors" value="football" />足球
            <input type="checkbox" name="favors" value="music" />音乐 
            <input type="checkbox" name="favors" value="swim" />游泳<br /> 
            <select name="cities">
                <option value="1">内蒙</option>
                <option value="2">河南</option>
                <option value="3">河北</option>
            </select><br />
            <textarea rows="3" cols="3" name="description"></textarea>
            <br /> <input type="submit" value="提交" />
        </form>
    </body>
    </html>
    package com.fgy;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    @WebServlet("/Servlet2")
    public class Servlet2 extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("============================");
            //临时地解决一下控制台中文乱码问题
            request.setCharacterEncoding("utf-8");
            String userName=request.getParameter("username");
            System.out.println(userName);
            System.out.println(request.getParameter("userpwd"));
            System.out.println(request.getParameter("gender"));
            String[] favors=request.getParameterValues("favors");
            if (favors != null) {
                for (String s : favors) {
                    System.out.println(s);
                }
            }
            System.out.println(request.getParameter("cities"));
            System.out.println(request.getParameter("description"));
            
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
    servlet处理文件上传-1
    	文件上传方式概述
    	客户端编程要点
    	servlet内置的文件上传配置
    	使用part处理文件上传
    	
    
    	part接口及获取其实例
    		在multipart请求中,每一个表单控件,都会被转换成一个part
    		使用HttpServletRequest获取Part:
    			getPart:返回指定名称的part,一个文件
    			getParts:返回请求中所有的part,多个文件
    		part接口的方法
    		
    		part接口方法2
    			write
    			delete
    			getInputStream
    servlet处理文件上传-2
    	文件上传的相关配置
    	服务器端处理文件上传要点
    	
    	@MultipartConfig注解及属性
    	maxRequestSize
    	maxFileSize
    	location
    	fileSizeThreshold
    	
    	
    	如果location没有指定的话,就在下面这个路径,
    	全局属性的值javax.servlet.context.tmpdir
    	apache-tomcat-8.5.15workCatalinalocalhostproject3
    	
    	如果在web.xml中配置,就要用到multipart的子标签,就是上面几个属性
    前端展示页面

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="Upload2" method="post" enctype="multipart/form-data"> <input type="file" name="file1"><br> <input type="file" name="file2"><br> <input type="text" name="name"><br> <input type="submit" value="上传"> </form> </body> </html> ================================== 后端处理逻辑 package com.fgy; import java.io.IOException; import java.util.Collection; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; /** * Servlet implementation class UploadServlet */ @WebServlet("/Upload2") @MultipartConfig(maxRequestSize=1024*1024*10,maxFileSize=1024*1024*9,location="d:\") public class UploadServlet2 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("========================="); System.out.println(request.getParameter("name")); Collection<Part> parts=request.getParts(); for (Part part : parts) { if (part.getContentType() != null) { String fileName=part.getSubmittedFileName(); if (!fileName.equals("")) { part.write(fileName); } } } } }
  • 相关阅读:
    Scala 插入排序
    win10下安装使用mysql-8.0.18
    Autofac注入多数据库(DbContext)
    缓存一致性问题以及方案(一) Redis
    Java编程中,一些好的习惯从一开始就值得坚持
    IDEA项目无法引用本地Class类,引用路径正确但报错标红
    git怎么撤销已经push到远端的commit?
    git的命令大全及如何修改git账号和提交的邮箱和用户名
    renren-fast-vue无法运行相关问题解决办法n ./src/assets/scss/index.scss Module build failed: Error: ENOENT: no su
    VsCode系列(一):下载安装及汉化
  • 原文地址:https://www.cnblogs.com/createyuan/p/9083090.html
Copyright © 2011-2022 走看看