最终我还是踏上了S2SH的自学路程,本来纠结于WEB开发的麻烦,所以都没怎么往这方面靠,跟J2EE的东西了解的很少,但是鉴于需求驱动不得不踏上这个路,好麻烦啊。- -,而且知识点很多,啥都要用到,所以得常写博客做笔记,以供以后回顾起来会比较方便。


项目一,简单的加法。

主要就是实现加法运算,熟悉下手感。

172110254.png

分析下和以前JAVA项目有什么不同,大概就是多了JAVA EE 5.0这个库,然后里面提供了一些类接口。

然后就是WebRoot这个文件夹,WEB-INF里面保存一些lib啊,web.xml(这个是配置文件),还有自己生成的index.jsp主页。

META-INF 目录
  大多数 JAR 文件包含一个 META-INF 目录,它用于存储包和扩展的配置数据,如安全性和版本信息。
Java 2 平台识别并解释 META-INF 目录中的文件和目录,以便配置应用程序、扩展和类装载器. from:http://blog.sina.com.cn/s/blog_5656b00201009wmn.html










web.xml

?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Controller</servlet-name>
    <servlet-class>servlet.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/servlet/Controller/aa.aciton</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这里可以配置servlet相关信息

创建一个servlet基本步骤应该是这样的

1.创建一个class Controller extends HttpServlet

Controller里面主要实现了下面两个方法:

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException

2.在web.xml配置文件了配置相关信息,<servlet></servlet> <servlet-mapping></servlet-mapping>主要是这2个

3.然后大概就O啦


jsp文件

<form id="calcForm" method="post" action="servlet/Controller/aa.aciton">
  <input type="text" value="0" name="text1"><br>
    This is my JSP page. <br>
   <input type="text" value="0" name="text2"><br><input type="submit" value="add" /><br>
   </form>


自我理解:

当点击按钮的时候,java ee 包里面的类会 得到该页面提交的post请求,然后它会根据表单上面的method方法和action以及web.xml里面servlet的配置信息,去调用对应的Controller类里面的

post或者get方法并传递相应的2个参数,然后在这两个函数里面就可以为所欲为了。

PS:通过request.getServletPath();即可获得action里面的值

Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.

This method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.


Returns:

a String containing the name or path of the servlet being called, as specified in the request URL, decoded, or an empty string if the servlet used to process the request is matched using the "/*" pattern.


form是一个表单
action是转向地址,即form表单需要提交到哪里

174101288.pngfrom:http://www.w3school.com.cn/tags/att_form_action.asp