初识javaWeb的创建
准备工作:
- 首先在自己的电脑上安装tomcat
- 安装能编写javaee的eclipse或者myeclipse
进入eclipse:
设置编码格式:
设置默认浏览器
设置项目在服务器目录下的位置:
1.单击Servers下的服务器选择open
2.在创建项目前可修改:Use Tomcat installtion
创建项目:
1.选择other...
2.创建 Dynmic Web Project
3.需要勾选创建xml的选项,不然不会创建web.xml文件
创建一个MyServlet
MyServlet:
注意:
1.运行通过<servlet-mapping>url-pattern --> <servlet-mapping>servlet-name ---><servlet>servlet-name --> <servlet>servlet-class
2.@webservlet("MyServlet") html页面中action填MyServlet
<?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>firstjavaweb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>My</servlet-name>
<servlet-class>firstjavaweb.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>My</servlet-name>
<url-pattern>/c</url-pattern>
</servlet-mapping>
</web-app>
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//这个说的是设置请求体里面的文字编码。 get方式,用这行,有用吗? ---> get
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
//test01(username, password);
//post过来的数据乱码处理:
System.out.println("post : userName="+username+"==password="+password);
}
/**
* 处理get请求过来的数据乱码
* @param username
* @param password
* @throws UnsupportedEncodingException
*/
private void test01(String username, String password) throws UnsupportedEncodingException {
System.out.println("userName="+username+"==password="+password);
//get请求过来的数据,在url地址栏上就已经经过编码了,所以我们取到的就是乱码,
//tomcat收到了这批数据,getParameter 默认使用ISO-8859-1去解码
//先让文字回到ISO-8859-1对应的字节数组 , 然后再按utf-8组拼字符串
username = new String(username.getBytes("ISO-8859-1") , "UTF-8");
System.out.println("userName="+username+"==password="+password);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("来了一个post请求...");
doGet(request, response);
}
}
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
执行登录:<br>
<form action="MyServlet" method="post">
账号:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type="submit" value="登录"/>
</form>
</body>
</html>
运行结果:
来了一个post请求...
post : userName=arga==password=aerg