1.题目:get post请求
根据请求参数返回不同的页面
http://ip:prot/server?opt=a 返回html文件
opt=b 返回图片 opt=c 返回 text
package com.yq.work;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
get请求:
/*
get post请求
根据请求参数返回不同的页面
http://ip:prot/server?opt=a 返回html文件 opt=b 返回图片 opt=c 返回 text
*/
@WebServlet(urlPatterns = "/opt")
public class work1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String opt = req.getParameter("opt");
if(opt.equals("a")){
System.out.println("doGet 处理请求,返回html文件");
resp.getWriter().append("<html><head><meta charset='UTF-8'></head><body><h3>doGet处理返回结果</h3></body></html>");
}else if(opt.equals("b")){
System.out.println("doGet 处理请求,返回图片");
resp.setContentType("image/jpg");
//获取图片输入流
InputStream in = getServletContext().getResourceAsStream("images/猪小屁2.jpg");
ServletOutputStream out = resp.getOutputStream();
byte[] data = new byte[4096];
int len;
while ((len = in.read(data)) != -1){
out.write(data,0,len);
}
out.close();
in.close();
}else if(opt.equals("c")){
System.out.println("doGet 处理请求,返回text");
resp.setContentType("text/html");
}
}
}
post请求:
package com.yq.work;
/*
post 注册 name phone sex salary job 返回html 提示添加完成消息
*/
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/login2")
public class work12 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
String phone = req.getParameter("phone");
String sex = req.getParameter("sex");
String salary = req.getParameter("salary");
String job = req.getParameter("job");
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().append("<html>" +
"<head>"+"<meta charset='UTF-8'>"+"</head>"+
"<body>"+"<h3>添加完成</h3><hr>"+
"<p>"+name+"</p>"+"<p>"+phone+"</p>"+"<p>"+sex+"</p>"+"<p>"+salary+"</p>"+"<p>"+job+"</p>"+
"</body>"+
"</html>");
}
}
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>get 请求</h3>
<hr>
<form action="opt">
<p><input type="text" name="opt"></p>
<p><input type="submit" value="get请求"></p>
</form>
</body>
</html>
运行结果:
2.转发 重定向
http://ip:prot/hand?forward=a 转发 html
forward=b进入淘宝网站
package com.yq.work;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
转发 重定向
http://ip:prot/hand?forward=a 转发 html
forward=b进入淘宝网站
*/
@WebServlet(urlPatterns = "/work2")
public class work2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String option = req.getParameter("option");
if(option.equals("a")){
//请求转发
req.getRequestDispatcher("/work2.html").forward(req,resp);
}else if(option.equals("b")){
resp.sendRedirect("https://www.taobao.com");
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>验证a或b</h3>
<hr>
<form action="work2" method="get">
<p><input type="text" name="option"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>转发 html</h3>
</body>
</html>
运行结果:
3.配置参数 初始化参数 username 获取初始化参数 输出
package com.yq.work;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
配置参数 初始化参数 username 获取初始化参数 输出
*/
@WebServlet(name = "hello",urlPatterns = "/x",loadOnStartup = -1,initParams = {@WebInitParam(name = "username", value = "html")})
public class work3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("初始化参数 = " + getInitParameter("username"));
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>初始化参数</h3>
<hr>
<form action="x">
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
运行结果:
4.异常处理 出现异常 转入提示页面
package com.yq.work;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
异常处理 出现异常 转入提示页面
*/
@WebServlet(urlPatterns = "/exc")
public class work4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>异常</h3>
<hr>
<form action="exc" method="post">
<p><input type="text" name="name"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
运行结果: