制作登陆界面

login.html

<!DOCTYPE html>
<html>
<head>
<title>login.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body >
<div style="text-align: center;">
<form action="./servlet/DemoServlet" enctype="application/x-www-form-urlencoded">
用户名:<input type="text" name="username"/><br/><br/> 密 码: <input type="password" name="userpass"/><br/><br/>
<input type="submit" name="" value="登陆">
</div>
</form>
</body>
</html>

user.html

<!DOCTYPE html>
<html>
<head>
<title>user.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
<a href="#">查询用户信息</a>
</body>
</html>

DemoServlet.java

package www.csdn.net.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; public class DemoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//请求中带有的参数值
String uName=request.getParameter("username");
String uPass=request.getParameter("userpass");
System.out.println("用户名"+uName);
System.out.println("密码"+uPass);
System.out.println(request.getRemoteAddr()+"------"+request.getRemoteHost()); //响应
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
/* PrintWriter out = response.getWriter();
out.println("欢迎"+"----"+request.getRemoteAddr()+uName+"登陆此系统");
out.flush();
out.close();*/
//用户名密码存在302临时重定向
response.setStatus(302);//状态码
System.out.println(request.getContextPath());
response.sendRedirect(request.getContextPath()+"/user.html");//跳转另一个界面 } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
} }

Demo1Servlet.java怎样写入图片

package www.csdn.net.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Demo1Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
downImg(request, response);
}
public void showImg(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应的数据是图片
response.setContentType("image/jpeg");
//写出图片//获取项目的绝对路径
String path=this.getServletContext().getRealPath("/");
//图片路径
File file=new File(path+"/image/150CA0R1YPM.jpg");
System.out.println(file);
//创建文件的输入流
FileInputStream is=new FileInputStream(file);
//响应的输出流
ServletOutputStream out=response.getOutputStream();
//读取的缓冲区
byte buffer[]=new byte[1024];
//记录读取的长度
int len=0;
//读取 如果len=-1读取完毕
while((len=is.read(buffer))!=-1){
//写入
out.write(buffer, 0, len);
}
//释放资源操作
is.close();
out.flush();
out.close();
}
/**
* 下载图片
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void downImg(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//写出图片//获取项目的绝对路径
String path=this.getServletContext().getRealPath("/");
//图片路径
File file=new File(path+"/image/小婷.jpg");
System.out.println(file.getName());
//创建文件的输入流
FileInputStream is=new FileInputStream(file);
response.setHeader("Content-Disposition", "attachment;filename="+file.getName());
//响应的输出流
ServletOutputStream out=response.getOutputStream();
//读取的缓冲区
byte buffer[]=new byte[1024];
//记录读取的长度
int len=0;
//读取 如果len=-1读取完毕
while((len=is.read(buffer))!=-1){
//写入
out.write(buffer, 0, len);
}
//释放资源操作
is.close();
out.flush();
out.close();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doGet(request, response);
} }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name> <servlet>
<servlet-name>DemoServlet</servlet-name>
<servlet-class>www.csdn.net.servlet.DemoServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Demo1Servlet</servlet-name>
<servlet-class>www.csdn.net.servlet.Demo1Servlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>DemoServlet</servlet-name>
<url-pattern>/servlet/DemoServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Demo1Servlet</servlet-name>
<url-pattern>/servlet/Demo1Servlet</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

怎样运用servlet的更多相关文章

  1. servlet文件下载

    创建web工程servlet,新建DownloadServlet.java package com.xmyself.servlet; import java.io.File; import java. ...

  2. java中servlet的各种路径

    1. web.xml中<url-pattern>路径,(叫它Servlet路径!) > 要么以“*”开关,要么为“/”开头 2. 转发和包含路径 > *****以“/”开头:相 ...

  3. Servlet监听器笔记总结

    监听器Listener的概念 监听器的概念很好理解,顾名思义,就是监视目标动作或状态的变化,目标一旦状态发生变化或者有动作,则立马做出反应. Servlet中的也有实现监听器的机制,就是Listene ...

  4. JavaWeb——Servlet

    一.基本概念 Servlet是运行在Web服务器上的小程序,通过http协议和客户端进行交互. 这里的客户端一般为浏览器,发送http请求(request)给服务器(如Tomcat).服务器接收到请求 ...

  5. servlet 简介,待完善

    什么是Servlet?① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③ 这个在服务器端运行,用以处理客户端的请求 Servlet相关包的介绍--javax. ...

  6. java web学习总结(五) -------------------servlet开发(一)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  7. servlet使用入门

    创建web工程servlet,然后新建TestServlet.java package com.xmyself.servlet; import java.io.IOException; import ...

  8. 基于jsp+servlet图书管理系统之后台万能模板

    前奏: 刚开始接触博客园写博客,就是写写平时学的基础知识,慢慢发现大神写的博客思路很清晰,知识很丰富,非常又价值,反思自己写的,顿时感觉非常low,有相当长一段时间没有分享自己的知识.于是静下心来钻研 ...

  9. [Servlet] 初识Servlet

    什么是Servlet? 定义 Servlet的全称是 Server Applet,顾名思义,就是用 Java 编写的服务器端程序. Servlet 是一个 Java Web开发标准,狭义的Servle ...

  10. Java Servlet+Objective-c图上传 步骤详细

    一. Servlet 1.创建图片保存的路径 在项目的WebContent下创建一个上传图片的专属文件夹. 这个文件夹创建后,我们保存的图片就在该文件夹的真实路径下,但是在项目中是无法看到上传的图片的 ...

随机推荐

  1. Java前期(静态)绑定和后期(动态)绑定

    Java前期(静态)绑定和后期(动态)绑定 程序绑定的概念:绑定指的是一个方法的调用与方法所在的类(方法主体)关联起来.对java来说,绑定分为静态绑定和动态绑定:或者叫做前期绑定和后期绑定. 静态绑 ...

  2. PHP数组键值使用单引号和双引号和无符号的区别

    PHP数组键值使用单引号和双引号和无符号的区别 方法/步骤 1 第一种:$array['key']此单引号键值模式可以直接被解析为一个数组即$array 第二种:$array["key&qu ...

  3. 使用WebClient與HttpWebRequest的差異

    在<Windows Phone 7-下載檔案至Isolated Storage>提到了透過WebClient的功能將網站上的檔案下載至 WP7的Isoated Storage之中.但實際的 ...

  4. 温故而知新-面向对象的PHP

    1 类的多态 不同的类对同一操作可以有不同的行为. 比如自行车和汽车都有移动这个成员函数行为, 那么自行车类可以移动,行为和汽车的移动行为肯定不同. 2 析构函数不能有参数 3 __set和__get ...

  5. uwsgi的使用

    uwsgi是一个WEB服务器,只要用于python部分,类似于nginx ,apache 1 使用pip命令安装 pip install uwsgi 安装成功以后 可以做一个简单的测试 2 新建一个t ...

  6. 关于eclipse open call hierarchy功能的一个细节

    这个功能对应的快捷键是ctrl alt H,大家应该都很熟悉了.默认是查找这个方法的被调用堆栈.90%的人应该也是习惯这个默认的功能的,也基本无视它的另一个功能. 昨天重启eclipe之后,我的ecl ...

  7. 消息队列kafka

    消息队列kafka   为什么用消息队列 举例 比如在一个企业里,技术老大接到boss的任务,技术老大把这个任务拆分成多个小任务,完成所有的小任务就算搞定整个任务了. 那么在执行这些小任务的时候,可能 ...

  8. 利用CSS3 filter:drop-shadow实现纯CSS改变图片颜色

    体验更优排版请移步原文:http://blog.kwin.wang/programming/css3-filter-drop-shadow-change-color.html 之前做项目过程中有时候遇 ...

  9. FDStoredProc 存储过程控件 强

    //FireDAC FDStoredProc1->StoredProcName = "myReport1"; FDStoredProc1->Prepare(); FDS ...

  10. Spring Data JPA 基本使用

    Spring Data 简化开发,支持Nosql和关系型数据库, DEMO https://github.com/easonstudy/boot-demo/tree/master/boot-sprin ...