【步骤2】编写,其代码如下:
import.*;
import.*;
import.*;
publicclassParamServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
("gb2312");
("text/html;charset=gb2312");
Stringname=("name");
name=newString("iso8859-1"),"gb2312");
Stringage=("age");
age=newString("iso8859-1"),"gb2312");
PrintWriterpw=();
("");
("
");("");
("
");("
姓名:
"+name+"
");
("
年龄:
"+age+"
");
("");
("");
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
}
【步骤3】helloapp/。
输入数据,点击“确定”按钮,体会表单参数的传递。
问题1:
如何通过超链接的锚标签()传递请求参数
(三)编写一个程序实现文件上传功能。
假设学生上传作业,需要执行学号和上传的文件。
:
import.*;
import.*;
import
import
@WebServlet(name="as11",urlPatterns={"/"})
@MultipartConfig(location="D:
\\",fileSizeThreshold=1024)
publicclassas11extendsHttpServlet{
publicvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
etRealPath("/");
Stringmnumber=("mnumber");
Partp=("fileName");
Stringmessage="";
if()>1024*1024){;
import.*;
import
import
import
@WebServlet(name="FirstServlet",urlPatterns="/first")
publicclassFirstServletextendsHttpServlet{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
ServletContextcontext;
publicvoidinit(){
context=getServletContext();
}
publicvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
("gb2312");
Stringsnumber=("snumber");
Stringsname=("sname");
Studentstud=newStudent(snumber,sname);
("stud",stud);
RequestDispatcherview=
("/SecondServlet");
(request,response);
}
}
:
package;
import.*;
import.*;
import
import
publicclassSecondServletextendsHttpServlet{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
ServletContextcontext;
publicvoidinit(){
context=getServletContext();
}
publicvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
Studentstud=(Student)("stud");
("text/html;charset=gb2312");
PrintWriterout=();
("
");("
");("学号:
"+()+"
");
("姓名:
"+()+"
");
("返回输入页面");
("
");("");
}
}
:
xsi=xmlns=xsi:
schemaLocation=id="WebApp_ID"version="">
ss
FirstServlet
FirstServlet
/FirstServlet
SecondServlet
SecondServlet
/SecondServlet
四.实验体会(包括收获、心得体会、存在的问题及解决问题的方法、建议等)
通过本次实验,对HTTP请求对及数据处理有了一定的了解和掌握,对javaweb编程更加熟悉。
比较容易出现404的问题,通过学习与仔细观察,处理问题的能力得到了一定的增强。
五.思考题
1.在Servlet中如何获得请求参数
答:
web组件之间参数传递有3种方式:
(1)("name");
a、通过超链接
b、通过表单
c、通过servlet转发或重定向
d、通过隐藏域完成
(2)("key");如何实现请求转发如何在请求对象中共享数据
jsp:
("/*.jsp").forward(request,response);
servlet:
sendRedirect()方法原理:
?
1,客户端发送请求,Servlet1做出处理。
?
2,Servlet1调用sendReadirect()方法,将客户端的请求重新定位到Servlet2。
?
3,客户端访问Servlet2.?
4,Servlet2对客户端浏览器做出响应。
forward()方法原理:
?
1,客户端发送请求,Servlet1做出处理。
?
2,Servlet调用forward()方法,将请求转发给Servlet2来处理请求,为客户端服务.
3,Servlet2对客户端浏览器做出响应。
<:
forward/>或include/>
("").forward();
("").include();