Eclipse开发web项目
在Eclipese中添加Tomcat
进行Add Tomcat服务器,记得在server窗口中也要添加一下
新建web项目
- new 新建Dynamic Web project
- 在webcontent下面创建index.jsp
- 在浏览器中输入http://localhost:8080/Mystudy/index.jsp 进行访问项目
WEB-INF无法通过浏览器直接访问,只能通过请求转发来访问
修改Eclipse中Tomcat的地址
在Eclipse中编辑html文件实时预览
右键单击html文件 选择open with web page editor
页面中form表单的action填写如下
编码问题
设置requset编码
request.setCharacterEncoding("utf-8");
或者
String name = new String(request.getParameter("username").getBytes("ISO8859-1"),"utf-8");
设置response编码
response.setContentType("text/html;charset=utf-8");
JDBC连接Mysql数据库
配置Struts2
struts2.5 版本的web.xml 写法
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml的写法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<package name="myPackage" extends="struts-default">
<action name="hello">
<result>/success.jsp</result>
</action>
</package>
</struts>
web.xml 配置
配置错误页面,将以下代码片段添加到<web-app>
标签里
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>