怎样运用servlet
制作登陆界面
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>