1 Servlet使用方法
package com.yuxhu.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;
/* 1.创建一个类去继承HttpServlet
* 2.重写doGet/doPost头方法
*
* HttpServletRequest : 用来接受数据 (读)
* HttpServletResponse : 用来响应数据 (写)
*
* 直接运行是不行的,需要配置访问路径
* 1.注解
* 2.web.xml文件配置
* 3.启动服务器之后页面怎么访问?
* localhost:8080/项目/访问路径(唯一)
*
*/
// @WebServlet(value="/yxh") //由于设置了servlet类,浏览器默认指向这里,所以不需要注解
public class ServletXml extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收请求 HttpServletRequest
String username = req.getParameter("username");
String password = req.getParameter("password");
String user = new String(username.getBytes("iso-8859-1"),"utf-8");//解决乱码
String pwd = new String(password.getBytes("iso-8859-1"),"utf-8");//解决乱码
//响应结果 HttpServletResponse
resp.setContentType("text/html;charset=utf-8"); //解决乱码
PrintWriter pw = resp.getWriter();
pw.write("账号:" + user + "<br>");
pw.write("密码:" + pwd);
//控制台
System.out.println("账号:" + user);
System.out.println("密码:" + pwd);
}
}
2 设置 servlet 类 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Servlet1</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置 servlet类-->
<servlet>
<!-- servlet-name 名字随便取 -->
<servlet-name>xml</servlet-name>
<!-- 全类名 : 包名+类名 -->
<servlet-class>com.ithc.demo.ServletXml</servlet-class>
</servlet>
<!--配置映射 -->
<servlet-mapping>
<!-- 需要与上面的name值一样 -->
<servlet-name>xml</servlet-name>
<!-- 配置访问路径 ,整个项目里面要唯一 -->
<url-pattern>/xml</url-pattern>
</servlet-mapping>
</web-app>
3 浏览器页面
<%@ 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>
<!--
注释快捷键 : ctrl + shift + ?
action : 访问的路径
method : 访问的方法
//以下语句注释也会打印出来
${pageContext.request.contextPath}//引用名字 可替换下面的xml
-->
//form表单
<form action="xml" method="post">
<input type="text" name="username" placeholder="用户名">
<br/>
<input type="password" name="password" placeholder="密码">
<br/>
<input type="submit" value="确定">
</form>
</body>
</html>
4 单选,多选,超链接
表单
<%@ 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>
<form action="demo03" method>
<!-- 多选框 -->
<input type="checkbox" name="shop" value="mi">小米
<br>
<input type="checkbox" name="shop" value="huawei" checked="checked">华为
<br>
<input type="checkbox" name="shop" value="iphone">苹果
<br>
<!-- 单选框 -->
<input type="radio" name="gender" value="男" checked="checked">男
<input type="radio" name="gender" value="女">女
<input type="submit" name="确定">
</form>
<!-- 超链接 -->
<a href="demo03?gender=男">点击</a>
</body>
</html>
servlet
<%@ 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>
<form action="demo03" method>
<!-- 多选框 -->
<input type="checkbox" name="shop" value="mi">小米
<br>
<input type="checkbox" name="shop" value="huawei" checked="checked">华为
<br>
<input type="checkbox" name="shop" value="iphone">苹果
<br>
<!-- 单选框 -->
<input type="radio" name="gender" value="男" checked="checked">男
<input type="radio" name="gender" value="女">女
<input type="submit" name="确定">
</form>
<!-- 超链接 -->
<a href="demo03?gender=男">点击</a>
</body>
</html>