坚持 成长 每日一篇
启动Tomcat并打开控台输出日志
- 打开终端cd 到启动脚本路径如下:
$ cd /Users/chris/Documents/apache-tomcat-8.0.23/bin
然后运行启动脚本$ sh startup.sh
- cd 到Tomcat文件下的logs文件如:
$ cd /Users/chris/Documents/apache-tomcat-8.0.23/logs
运行如下命令打开tomcat控台$ tail -f catalina.out
打开后保持终端不变,下面我们开始布置一个Servlet
布置一个Servlet
1、写一个简单的HTML,也可以复制一个写好的HTML,我们用post提交方式,当然也可以用get提交,大家可以自己尝试一下
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录</title>
</head>
<body>
<form action="http://localhost:8080/loginServlet/LoginServlet" method="post">
用户:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
<input type="submit" value="登录" />
</form>
</body>
</html>
2、在Web项目中新建一个类LoginServlet,输入下面代码即可
//引入所需要的包
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class LoginServlet extends HttpServlet {
//重写doGet方法
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("登录名:" + username);
System.out.println("密码:" + password);
//获取用户请求的方式
System.out.println("客户端请求的方式:" + request.getMethod());
System.out.println("客户端请求的URL:" + request.getRequestURL());
System.out.println("客户端的IP地址为:" + request.getRemoteAddr());
System.out.println("客户端的主机名:" + request.getRemoteHost());
System.out.println("客户端的用户名:" + request.getRemoteUser());
System.out.println("客户端的端口号:" + request.getRemotePort());
System.out.println("本地服务器的IP地址:" + request.getLocalAddr());
Enumeration<String> paraNames=request.getParameterNames();
//此处打印出所有的请求参数,如果iOS开发测试自己的参赛到服务器是否正确,可以自己弄一个Tomcat测试一下
for(Enumeration e=paraNames;e.hasMoreElements();){
String thisName=e.nextElement().toString();
String thisValue=request.getParameter(thisName);
System.out.println(thisName+"--------------"+thisValue);
}
//服务器端打印信息
//System.out.println("username=" + username);
//System.out.println("password=" + password);
//设置编码格式
response.setContentType("text/html;charset=utf8");
//返回html页面
response.getWriter().println("<html>");
response.getWriter().println("<head>");
response.getWriter().println("<title>登录信息</title>");
response.getWriter().println("</head>");
response.getWriter().println("<body>");
response.getWriter().println("欢迎【" + username + "】用户登录成功!!!");
response.getWriter().println("</body>");
response.getWriter().println("</html>");
}
//重写doPost方法
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
doGet(request, response);
}
}
3、在Tomcat的webapp文件下新建一个loginServlet文件夹,打开loginServlet文件,把刚才写的HTML文件移动到该文件目录下,同时新建一个WEB-INF文件夹,打开文件夹新建一个web.xml,配置文件内容如下。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
3、打开浏览器输入http://localhost:8080/LoginServlet/login.html我们就会打开刚才写html文件,点击登陆我们会在终端的控台看到如下内容
登录名:
密码:
客户端请求的方式:POST
客户端请求的URL:http://localhost:8080/loginServlet/LoginServlet
客户端的IP地址为:0:0:0:0:0:0:0:1
客户端的主机名:0:0:0:0:0:0:0:1
客户端的用户名:null
客户端的端口号:58785
本地服务器的IP地址:0:0:0:0:0:0:0:1
username--------------
password--------------