练习
1.get post请求
根据请求参数返回不同的页面
http:l/ip:prot/server?opt=a返回html文件 opt=b返回图片opt=c返回text
RespServlet .java
@WebServlet(urlPatterns = "/resp")
public class RespServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// FileInputStream in = new FileInputStream("images/s.png");
req.setCharacterEncoding("UTF-8");
String opt = req.getParameter("opt");
if (opt.equals("b")) {
resp.setContentType("image/png");
InputStream resourceAsStream = getServletContext().getResourceAsStream("images/s.png");
byte[] date = new byte[4096];
int len;
ServletOutputStream out = resp.getOutputStream();
while ((len = resourceAsStream.read(date)) != -1) {
out.write(date, 0, len);
}
out.close();
resourceAsStream.close();
}else if(opt.equals("c")){
resp.setContentType("text/plain;charset=UTF-8");
resp.getWriter().println("返回text类型");
}
}
}
post注册name phone sex salary job返回html提示添加完成消息
LoginServlet.java
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
resp.getWriter().println("doget请求");
System.out.println(username+":"+password);
resp.getWriter().println(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
resp.getWriter().println("dopost请求");
System.out.println(username+":"+password);
resp.getWriter().println(username+":"+password);
}
}
2.转发重定向
http://ip:prot/hand?forward= a转发html forwar = b 进入淘宝网站
ForwardServlet.java
@WebServlet(urlPatterns = "/hand")
public class ForwardServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String forward = req.getParameter("forward");
if (forward.equals("a")){
req.getRequestDispatcher("/login.html").forward(req,resp);//转发
}else if(forward.equals("b")){
resp.sendRedirect("https://www.taobao.com/");//重定向
}
}
}
3.配置参数初始化参数username获取初始化参数输出
web.xml 配置
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.yq.servlet.InitServlet</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>法外狂徒张三</param-value>
</init-param>
</servlet>
public class InitServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().println("username初始化参数:"+getInitParameter("username"));
}
}
4.异常处理出现异常转入提示页面
web.xml 配置
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/Init</url-pattern>
</servlet-mapping>
<error-page>
<error-code>500</error-code>
<location>/error.html</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/error.html</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/error.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>ERROR页面</h2>
<hr>
<h5>emmmm......资源跑到火星上去了</h5>
</body>
</html>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="resp">
<h2>输入:b返回图片 c返回text</h2>
输入:<input type="text" name="opt">
<button onclick="this.form.method='GET'">提交</button>
</form>
<form action="register">
<h2>注册</h2>
<p>名字<input type="text" name="name"></p>
<p>电话<input type="text" name="phone"></p>
<p>性别:男<input type="radio" name="sex" value="男" checked="checked">
女<input type="radio" name="sex" value="女"></p>
<p>工资<input type="number" name="salary"></p>
<p>职位<input type="text" name="job"></p>
<button onclick="this.form.method='POST'">提交</button>
</form>
<form action="hand">
<h2>输入:a转发b重定向到淘宝</h2>
<p>输入:<input type="text" name="forward"></p>
<button>提交</button>
</form>
<form action="Init">
<button>初始化参数</button>
</form>
<form action="ugbZXVPub">
<h2>出现异常转入提示页面</h2>
<button>异常处理</button>
</form>
</body>
</html>