JavaWeb-Servlet--day01(笔记)
程序员文章站
2022-03-15 09:24:57
...
传智播客-JavaWEB核心编程****
<!-- 配置和映射Servlet -->
<servlet>
<!-- Servlet的注册名 -->
<servlet-name>MyServlet</servlet-name>
<!-- Servlet的全类名 -->
<servlet-class>pack_Servlet.MyServlet</servlet-class>
<!-- 可以指定Servlet被创建的时机 -->
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
XML文件内容
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>pack_Servlet.MyServlet</servlet-class>
<!--配置初始数据-->
<init-param>
<param-name>name</param-name>
<param-value>Micheal</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>aaa@qq.com</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
Servlet编写内容
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init()...");
String user=servletConfig.getInitParameter("name");
System.out.println("user"+user);
Enumeration<String> names=servletConfig.getInitParameterNames();
while(names.hasMoreElements()) {
String name=names.nextElement();
String value=servletConfig.getInitParameter(name);
System.out.println("name:"+name+": Values:"+value);
}
System.out.println(servletConfig.getServletName());
}
输出结果:
XML代码
<context-param>
<param-name>address</param-name>
<param-value>NanJing</param-value>
</context-param>
<context-param>
<param-name>age</param-name>
<param-value>23</param-value>
</context-param>
Servlet代码:
ServletContext servletContext=servletConfig.getServletContext();
String address=servletContext.getInitParameter("address");
System.out.println("Address:"+address);
Enumeration<String> elements=servletContext.getInitParameterNames();
while(elements.hasMoreElements()) {
String name=elements.nextElement();
String value=servletContext.getInitParameter(name);
System.out.println("name:"+name+";value:"+value);
}
Servlet代码:
//只能查找WEB-INF目录内的文件路径,该路径指的是发布在服务器内的绝对路径
String path=servletContext.getRealPath("/web.xml");
System.out.println("Path"+path);
try {
ClassLoader classLoader=getClass().getClassLoader();
InputStream is=classLoader.getResourceAsStream("Mytest.properties");
System.out.println("1:"+is);
} catch (Exception e) {
e.printStackTrace();
}
try {
InputStream is2=servletContext.getResourceAsStream("Mytest.properties");//null,因为目录不对
System.out.println("2:"+is2);
InputStream is3=servletContext.getResourceAsStream("/WEB-INF/classes/Mytest.properties");
System.out.println("3:"+is3);
} catch (Exception e) {
e.printStackTrace();
}
输出结果
PathE:\Experiment\Java\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\day_01\web.xml
1:aaa@qq.com
2:null
3:aaa@qq.com
JSP界面:
<form action="/WebApplication2/NewServlet" method="post">
Name:<input type="text" placeholder="Pelase enter name" name="name"/>
Password:<input type="text" placeholder="Please your password" name="password"/>
<br/><br/>
Hobby:
<input type="checkbox" name="hobby" value="Music">Music
<input type="checkbox" name="hobby" value="Sprot">Sport
<input type="checkbox" name="hobby" value="Reading">Reading
<input type="checkbox" name="hobby" value="Game">Game
<br/>
<input type="submit" style="submit" class="login_btn" value="submit" />
</form>
Servlet界面
System.err.println("请求来了!!!");
System.err.println("request" + request);
//获取单个参数值
String name = request.getParameter("name");
String password = request.getParameter("password");
System.err.println("name:" + name + ";password:" + password);
//获取参数组
String hobby = request.getParameter("hobby");
System.err.println("hobby:" + hobby);
String[] hobbys = request.getParameterValues("hobby");
for (String element : hobbys) {
System.err.print("-->" + element);
}
//获取传过来的参数:getParameterNames()和getParameter()
//方法一:
Enumeration<String> names=request.getParameterNames();
while(names.hasMoreElements()){
String param_name=names.nextElement();
String param_values=request.getParameter(param_name);//注:若是checkbox,只能获取到首个参数
System.err.print("^^param_name:" + param_name+";param_values"+param_values);
}
//方法二
Map<String,String[]> map=request.getParameterMap();
for(Map.Entry<String,String[]> entry:map.entrySet()) {
System.err.print("**param_name:" + entry.getKey()+";param_values"+Arrays.asList(entry.getValue()));
}
Servlet代码:
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String requestURI = httpServletRequest.getRequestURI();//获取地址
System.err.println(requestURI);
String method = httpServletRequest.getMethod();//获取请求方式
System.err.println(method);
String queryString = httpServletRequest.getQueryString();//前提:GET请求,POST请求为NULL;获取地址栏中?后面的数据
System.err.println(queryString);
String servletPath = httpServletRequest.getServletPath();//获取当前Servlet的名称
System.err.println(servletPath);
//response.setContentType("text/html;charset=UTF-8");//默认的相应类型为html格式类型
response.setContentType("application/msword");//设置相应的类型为Word类型;
PrintWriter out = response.getWriter();//getWriter()方法
String name = request.getParameter("name");
out.println("Name:" + name);
上一篇: css怎么设置盒子两像素黑色边框
下一篇: css3 巧用结构性伪类选择器