对象HttpServletRequest 范围:
请求数据在httpservletRequest或者servletRequest的对象中以(键,值)表单的形式提供。
由于我们仅在服务或者doget或者dopolt方法中使用httpservletRequest对象,因此我们无法在相同servlet的其他方法中访问此对象。
因此,HTTPServletRequest对象的范围仅限于特定servlet的服务或者doget或者dopost方法。
如果Servlet组涉及处理单个客户端请求,那么所有servlet都可以在各自的服务或者doget或者dopost方法中使用httpservletRequest对象。
说明:
每当我们想要向服务或者DOGOST或者DOPOST方法发送临时结果或者本地数据到要访问的另一个servlet或者dogost方法时,强烈建议将临时数据以(键,值)的形式添加到请求对象。
方法:
public void setAttribute (String, Object); - 1 public Object getAttribute (String); - 2 public void removeAttribute (String); - 3 public Enumeration getAttributeNames (); - 4
方法-1用于以(键,值)对的形式将数据插入或者添加到httpservletRequest对象。
参数字符串表示键,参数对象表示密钥的值。
如果httpservletRequest对象中不存在密钥值,则数据将作为新条目添加。
如果httpservletRequest对象中已存在的键值,则键值保持不变,其值将被修改为新值。
例如:
req.setAttribute ("v1", new Integer (10));
req.setAttribute ("v2", "abc");
方法-2用于通过传递键的值来获取或者获取值的值。
如果在httpservletRequest对象中找不到键的值,则其返回类型(即,对象值)为null。
例如:
Object obj=req.getAttribute ("uname");
方法-3用于通过传递密钥的值来删除或者删除(键,值)对。
如果在HttpservletRequest对象中未创建密钥的值,则没有从httpservletRequest对象中删除任何内容。
例如:
req.removeAttribute (uname);
方法-4用于获取httpservletRequest对象中可用的键的名称。
例如:
Enumeration en=req.getAttributeNames ();
While (en.hasMoreElements ())
{
Object kname=en.nextElement ();
String key= (String) kname;
Object val=req.getAttribute (key);
}
getDataserv.java:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class GetDataServ extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String dno1 = req.getParameter("deptdetails_no");
int dno = Integer.parseInt(dno1);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Hanuman", "scott", "tiger");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from dept where deptno=" + dno);
rs.next();
String vdno = rs.getString(1);
String vname = rs.getString(2);
String vloc = rs.getString(3);
req.setAttribute("sdno", vdno);
req.setAttribute("sdname", vname);
req.setAttribute("sdloc", vloc);
ServletContext ctx = getServletContext();
RequestDispatcher rd = ctx.getRequestDispatcher("/ddserv");
rd.forward(req, res);
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req, res);
}
};
servlet属性范围:
变量或者属性的生命时间或者可见性称为范围。
在Servlet编程环境中,我们有3种类型的属性范围,它们是httpservletRequest范围,servletContext范围和会话范围。
示例
deptdetails.html:
<html>
<title>Retrieve department details</title>
<body bgcolor="#EEE8AA">
<center>
<form name="deptdetails" action="./gdserv" method="post">
<table border="1" bgcolor="#FFE4E1">
<tr>
<th>Enter department number : </th>
<td><input type="text" name="deptdetails_no" value=""></td>
</tr>
<tr>
<td align="center"><input type="submit" name="deptdetails_details" value="Details"></td>
<td align="center"><input type="reset" name="deptdetails_reset" value="Reset"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
web.xml:
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>GetDataServ</servlet-class>
</servlet>
<servlet>
<servlet-name>pqr</servlet-name>
<servlet-class>Ddserv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/gdserv</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>pqr</servlet-name>
<url-pattern>/ddserv</url-pattern>
</servlet-mapping>
</web-app>
ddserv.java:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Ddserv extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String rdno = (String) req.getAttribute("sdno");
String rdname = (String) req.getAttribute("sdname");
String rdloc = (String) req.getAttribute("sdloc");
pw.println("<h3>" + rdno + "</h3>");
pw.println("<h3>" + rdname + "</h3>");
pw.println("<h3>" + rdloc + "</h3>");
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req, res);
}
};
