Web开发需要常见的问题
1、sendRedirec()方法执行后,是会直接跳转到目标页面还是执行完其后的语句再跳转到目标页面???
该方法在执行完其后面的语句才会跳转到目标页面,比如:
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
req.setCharacterEncoding("utf-8");
String name = req.getParameter("userName");
if(name==null || name.equals(" ")){
resp.sendRedirec("input.jsp");
return; //调用return语句,结束doGet方法,避免后面的语句执行
}
PrintWriter out = resp.getWriter();
out.println("hello");
}
2 在web.xml配置文件中有如下配置,Servlet容器会创建几个HelloServlet实例???
<servlet>
<servlet-name>hello_zh</servlet-name>
<servlet-class>com.hzw.HelloServlet</servlet-class>
<init-param>
<param-name>language</param-name>
<param-value>中文</param-value>
</init-param>
<init-param>
<param-name>mesage</param-name>
<param-value>欢迎</param-value>
</init-param>
<servlet/>
<servlet-mapping>
<servlet-name>hello_zh</servlet-name>
<url-pattern>/zh/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>hello_en</servlet-name>
<servlet-class>com.hzw.HelloServlet</servlet-class>
<init-param>
<param-name>language</param-name>
<param-value>English</param-value>
</init-param>
<init-param>
<param-name>mesage</param-name>
<param-value>welcome</param-value>
</init-param>
<servlet/>
<servlet-mapping>
<servlet-name>hello_en</servlet-name>
<url-pattern>/en/hello</url-pattern>
</servlet-mapping>
Servlet容器是根据web.xml配置文件中Servlet的数量来创建Servlet实例,而不管Servlet类是不是同一个