当前位置:文档之家› WEB实验指导书

WEB实验指导书

实验一 JSP应用一、实验目的1.掌握JDK的安装配置2.熟悉Tomcat的配置3.测试JSP的运行环境4.掌握JSP的页面的基本结构5. 掌握JSP的指令标记和动作标记二、程序练习4-01.jsp4-02.jsp+4-03.jsp4-04.jsp4-05.jsp4-06.jsp4-07.jsp4-08.jsp4-09.jsp4-10.jsp4-14.jsp+4-15.jsp4-14.jsp+4-17.jspexam304.jsp+exam305.jsp比较该练习与4-10.jsp的区别。

4-19.jsp+4-20.jspCountV1.jsp4-23.jsp+4-24.jsp三、实验要求在input.htm页面中输入注册信息。

在handle.jsp中将用户输入的信息提取出来并输出。

buy1.jsp+buy2.jsp+display.jsp实验二 Servlet应用一、实验目的1.掌握Servlet API的基本构成、Servlet的生命周期2.熟悉Servlet 程序的编写与部署3. 了解过滤器的作用;4. 掌握过滤器的开发与部署的步骤;二、程序练习test3-02test3-03exam401.jsp+LoginServletexam202.jsp+RegServlet三、实验要求1、建立一个用户登录页面(login.html),让用户在该页面输入用户名、密码、性别、爱好,然后提交用户信息到名为LoginCl的Servlet,该Servlet的创建方法采用继承HttpServlet的方式实现,并在该Servlet的doPost()方法中获取用户信息,并利用HttpServletResponse对象的相应方法把用户信息发送到客户端浏览器显示给用户看。

2、掌握Servlet与JSP间的通信在名为RNDServlet的Servlet中随机产生10个0~1间的小数,传送给exam402.jsp在页面上显示。

3、编写一个过滤器改变请求编码。

【步骤1】编写一个loginform.html文件,代码如下:<html><head><title>使用过滤器改变请求编码</title><meta http-equiv="Content-Type" content="text/html;charset=GB2312"></head><body><center><h2>请输入用户名和口令:</h2><form method="post" action="servlet/check"><table><tr><td>用户名:</td><td><input name="name" type="text"></td></tr><tr><td>口令:</td><td><input name="pass" type="password"></td></tr><tr><td></td><td><input name="ok" type="submit" value="提交"><input name="cancel" type="reset" value="重置"></td></tr></table></form></center></body></html>【步骤2】编写处理请求参数的Servlet,代码如下:import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class CheckParamServlet extends HttpServlet{public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {String name = request.getParameter("name");String pass = request.getParameter("pass");response.setContentType("text/html;charset=gb2312");PrintWriter out = response.getWriter();out.println("<html><head><title>Param Test</title></head>");out.println("<h3 align=center>你的用户名为:"+name+"</h3>");out.println("<h3 align=center>你的口令为:"+pass+"</h3>");out.println("</body></html>");}public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}【步骤3】修改web.xml文件,加入下面代码:<servlet><servlet-name>CheckParamServlet</servlet-name><servlet-class>CheckParamServlet</servlet-class></servlet><servlet-mapping><servlet-name>CheckParamServlet</servlet-name><url-pattern>/servlet/check</url-pattern></servlet-mapping>【步骤4】在浏览器的地址栏中输入下面URL:http://localhost:8080/ helloapp/loginform.html输入用户名和口令,如下图所示:loginform.html页面的运行结果然后点击“提交”按钮,经CheckParamServlet处理后返回的结果如下图所示::CheckParamServlet 程序的运行结果从这里我们可以看到,从服务器返回的汉字成了乱码。

原因是没有指定request的编码。

下面通过编写一个过滤器改变请求编码。

【步骤5】过滤器代码如下:package filter;import java.io.IOException;import javax.servlet.*;public class EncodingFilter implements Filter {protected String encoding = null;protected FilterConfig config;public void init(FilterConfig filterConfig) throws ServletException {this.config = filterConfig;// 得到在web.xml中配置的编码this.encoding = filterConfig.getInitParameter("Encoding");}public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException, ServletException {if (request.getCharacterEncoding() == null) {// 得到指定的编码String encode = getEncoding();if (encode != null) {//设置request的编码request.setCharacterEncoding(encode);response.setCharacterEncoding(encode);}}chain.doFilter(request, response);}protected String getEncoding() {return encoding;}public void destroy() {}}【步骤6】在web.xml文件中配置过滤器,加入下面代码:<filter><filter-name>EncodingFilter</filter-name><filter-class>filter.EncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>gb2312</param-value></init-param></filter><filter-mapping><filter-name>EncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>【步骤7】重复第(4)步操作,结果如下:EncodingFilter 程序的运行结果实验三 JavaBean的使用一、实验目的1.理解JavaBean 的作用2.能够定义JavaBean3.能在jsp 页面中使用JavaBean二、程序练习1.运行课件上的例题:根据程序中输入的半径,计算园的周长和面积j5-01.javamethod.jspmethod2.jsp2. 修改bean中的属性j5-02.jspAttr1.jspAttr3.jspAttr2.jspchap4-1.htmlUserBean.java4-1.jsp4.四则运算。

相关主题