本章主干知识点:
1、JavaWeb开发环境搭建;JavaWeb项目结构说明;
2、表单提交;URL编码的几种方式;
第 1 节1-关于开发工具说明:MyEclipse不过如此
第 2 节2-JavaWeb开发环境搭建
第 3 节3-一探JavaWeb的神奇
第 4 节4-编写第一个Servlet
第 5 节5-Servlet配置过程分析
第 6 节6-JavaWeb项目结构说明
第 7 节7-解放用户的表单提交
第 8 节8-关于表单提交的细节
第 9 节9-什么是URL编码
第 10 节10-代码中如何进行URL编码
JavaWeb开发环境配置
开发工具:MyEclipse
Eclipse是一个开发工具的框架,可以在Eclipse上装各种插件用来进行各种语言(C++、Java、PHP)的开发
MyEclipse也是一个“Eclipse IDE forJava Developers”再加上额外的一些JavaEE开发插件形成的收费版。
Web服务器和JavaWeb
不会自己用socket从头写网站、太累、并发性、安全性都很麻烦。
现成的Web服务器:WebLogic、JBoss、Tomcat。帮我们完成Socket基本的请求、静态文件的处理、并发性、安全性等。
先使用MyEclipse内置的Tomcat(正式运行的时候是运行在单独的Tomcat服务器软件中,MyEclipse只是开发工具),绕过初学者配置的坑。后面再单独讲单独Tomcat的安装、配置。
感受一下JavaWeb
Server视图中是服务器列表,找到MyEclipse,点右键【DebugServer】,在【Console】中如果出现“Unrecognized Windows Sockets error: 0: JVM_Bind”等错误,则表示这个服务器端口已经被占用,在服务器上点右键【Configure Server Connector】,在【Port Number】中改一个端口号重启即可。
停止服务器的方法;重启服务器。遇到问题先重启一下服务器试试。
在项目上点右键→【Debug as MyEclipse Server Application】,就会将项目部署到Tomcat,并且打开浏览器访问页面。如果不报错能访问就说明ok了。以调试方式启动的好处是“热部署”,还有问题就重启
在WebRoot下新建一个1.html,写入一点内容,然后访问http://127.0.0.1:端口号/contextroot/1.html
由此可见,Tomcat服务器帮我们完成静态文件的请求处理。
配置浏览器
MyEclipse默认采用内置浏览器打开网页,但是内置浏览器用起来不方便,而且没有开发人员用的调试工具,我们统一改成Chrome浏览器。
Eclipse中Preferences→General→Web Browser。勾选【Useexternal Web Browser】,点击【New】创建一个新的浏览器设置。然后勾选这个浏览器
Servlet入门
Servlet是Java中对用户请求进行处理的基本元素,当用户请求服务器的时候,会按照匹配规则寻找Servlet,如果找到了Servlet则调用它的service方法,进行处理。
【Demo1Servlet.java】
package com.rupeng.test1;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo1Servlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
int i = Integer.parseInt(req.getParameter("i"));
int j = Integer.parseInt(req.getParameter("j"));
int r = i+j;
resp.getWriter().print(r);
}
}
WEB-INF/web.xml中添加配置
【web.xml】
<servlet>
<servlet-name>demo1</servlet-name>
<servlet-class>com.rupeng.test2.Demo1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo1</servlet-name>
<url-pattern>/demo1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo1</servlet-name>
<servlet-class>com.rupeng.test2.Demo1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo1</servlet-name>
<url-pattern>/haha</url-pattern>
</servlet-mapping>
为什么不搞成一组?因为可以多个servlet-mapping对应一个servlet。
项目目录结构
WebRoot是项目的根目录,放到这里的html、js、jsp等都可以直接通过路径访问,还可以访问子文件夹中的内容, WEB-INF、 META-INF等特殊文件夹除外。
可以把jar包放到WEB-INF 中的lib
Servlet内容是通过web.xml文件映射的,用户并不能直接访问java代码。
修改web.xml或者修改代码之后,tomcat服务器会根据情况重启、重启部署或者重新加载Context(会有延迟)。如果有问题就尝试手动重启一下Tomcat。
表单提交
Html表单<form>可以自动给服务器提交参数(get是通过url,post是通过报文体,后面会讲区别),不用用户自己拼url。action指定把表单内容提交给谁。
浏览器向服务器端提交数据,被提交数据的表单(input、select、textarea等)放到form中,
form中通过action属性设定表单被提交给哪个页面,为了在服务端取出表单项的值,需要在HTML中为表单元素设定name属性。"name=value",多个键值对之间用&分隔。
注意id是给JS操作Dom用的,name才是提交给服务器用的。id不能重复,name可以重复,重复的name的值都会被提交给服务器。
服务器端用req.getParameter("name");来根据表单项的name来获得提交的属性值。
checkbox没选中为null,选中为"on"。
【cf.html】
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>cf.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="multi" method="get">
<input type="text" name="i"/>+<input type="text" name="j"/><br/>
<input type="text" name="haha"/><br/>
<input type="text" name="haha"/><br/>
<input type="text" id="name"/><br/>
<input type="submit" name="btncheng" value="X"/><br/>
<input type="checkbox" name="cb1" value="vip"/>VIP会员<br/>
<input type="checkbox" name="cb2"/>外籍人士<br/>
</form>
</body>
</html>
【Demo2Servlet.java】
package com.rupeng.test1;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo2Servlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
int i = Integer.parseInt(req.getParameter("i"));
int j = Integer.parseInt(req.getParameter("j"));
int r = i*j;
resp.getWriter().print(r);
//String haha = req.getParameter("haha");
String[] hahas = req.getParameterValues("haha");
resp.getWriter().print("<br/>");
resp.getWriter().print(Arrays.toString(hahas));
String cb1 = req.getParameter("cb1");
if(cb1==null)
{
resp.getWriter().print("is not vip");
}
else if(cb1.equals("on"))
{
resp.getWriter().print(" vip");
}
String cb2 = req.getParameter("cb2");
if(cb2==null)
{
resp.getWriter().print("bushi waiji");
}
else if(cb2.equals("on"))
{
resp.getWriter().print("shi waiji");
}
}
}
强调
当点击【登录】按钮以后是浏览器将用户填写的文本框等控件中的值“提取”出来发送给服务器,而不是服务器来读取用户填写的这个页面
。
哪些标签的哪些值会被提交给服务器呢?将用户填写的内容提交到服务器有如下几个条件(使用浏览器监视网络请求验证):
1.只能为 input、textarea、select三种类型的标签(有name的value会被提交)
2.只有三种标签的value属性的值(select是选中项的value)才会提交给服务器。如果要将标签的value属性值提交到服务器,则必须为标签设定name属性,提交到服务器的时候将会以“name=value”的键值对的方式提交给服务器。name是给服务器用的,id是给Dom用的。
3.对于RadioButton,同name的为一组,选中的RadioButton的value被提交到服务器。
4.当input=submit的时候,只有被点击的按钮(要有name)的value才会被提交。
5.放到form标签内。
URL编码
1.如果url的name或者value中包含汉字、特殊符号(空格、+、<、>、=、/、?等,因为特殊符号有特殊含义)要进行URL编码:%+ascii码的十六进制。注意:别看骗人的地址栏,F12看报文。
2.比如“\”,它的ascii码是92,92的十六进制是5c,所以“\”的url编码就是%5c。
3.JavaScript可以用encodeURI、encodeURIComponent进行编码。区别:encodeURI是对于整个URL进行编码的,因此"; / ? : & = #"等是不进行编码的,因为这些在URL中都有特定含义; encodeURIComponent只是对“值”进行编码的,因此对于"; / ? : & = #"也会编码。分辨方法:Component(原件,部件)。反函数是decodeURI、decodeURIComponent
4.服务器端:URLEncoder.encode("你好<a b>","utf-8"),反函数是URLDecoder。相当于js中的encodeURIComponent
【Encode1.html】
<!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="encode1" method="get">
<input type="text" name="txt1"/>
<input type="submit" value="click"/>
</form>
<a href="" id="a1">click</a>
<input type="button" id="btn1" value="test"/>
<script type="text/javascript">
var btn1 = document.getElementById("btn1");
btn1.οnclick=function(){
/*
var a1 = document.getElementById("a1");
var txt1 = "a&b=3";
//alert(txt1);
alert(encodeURIComponent(txt1));
alert(encodeURI(txt1))
a1.href="encode1?txt1="+encodeURIComponent(txt1);*/
/*
var s = "http://www.rupeng.com/login?username=如鹏&password=a b";
alert(encodeURI(s));
alert(encodeURIComponent(s));*/
/*
var s = "http://www.rupeng.com/login?username=如鹏&password=a b";
var a1 = document.getElementById("a1");
a1.href=encodeURI(s);*/
var txt1 = "http://www.rupeng.com";
var a1 = document.getElementById("a1");
//a1.href="encode1?txt1="+encodeURIComponent(txt1);
a1.href="encode1?txt1="+encodeURI(txt1);
var s1 = encodeURIComponent(txt1);
alert(s1);
var s2 = decodeURIComponent(s1);
alert(s2);
};
</script>
</body>
</html>
package com.rupeng.test1;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Encode1Servlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String txt1 = req.getParameter("txt1");//对于浏览器提交的编码后的value,服务器会解码
resp.setContentType("text/html;charset=UTF-8");//不要让浏览器去猜“服务器返回给我的是什么格式”
//resp.setContentType("text/plain");//返回给你的就是普通文本,不要当成html去解析
resp.setCharacterEncoding("UTF-8");
byte[] bytes = txt1.getBytes("ISO-8859-1");
String utf8Txt1 = new String(bytes,"UTF-8");//就不乱吗了:先用ISO-8859-1得到bytes
//然后再用UTF-8解析得到新字符串
//resp.getWriter().println("<html><head><meta charset='utf-8'/></head><body>");
resp.getWriter().println("<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head><body>");
resp.getWriter().print("<strong>"+utf8Txt1+"</strong>");
resp.getWriter().print("</body></html>");
//String h = "a&b=3";
/*
String h = URLEncoder.encode("a&b=3", "UTF-8");
String html = "<a href='http://www.rupeng.com/a?un="+h+"'>aaa</a>";
resp.getWriter().print(html);
String h1 = URLDecoder.decode(h, "UTF-8");
resp.getWriter().print(h1);*/
}
}