servlet 3.0无需配置web.xml,使用注入方式配置servlet实现登陆功能(服务器需要支持servlet3.0)

首先申明上面的报错红叉,我也不知道怎么回事.总之能运行.
新建项目时选择java EE6.0,低版本没有servlet3.0.
先看一个基本示例.
Test.java是用来测试无需配置文件,无需静态页面(jsp,html)
直接访问servlet来从服务器上获取信息.
Test.java代码
package com.gys; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet(
name="Test",
urlPatterns={"/test"}
) public class Test extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println("<h1>思思博士</h1>");
}
}
访问结果:

看懂上面的name和urlpattern参数的,继续往下看.
servletConfigDemo.java代码
package com.gys; import java.io.IOException;
import java.util.Enumeration; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; @WebServlet(
urlPatterns={"/servletConfigDemo.do"},
loadOnStartup=1,
name="servletConfigDemo",
displayName="demo",
initParams={
@WebInitParam(name="success",value="success.html"),
@WebInitParam(name="error",value="error.html")
}
)
public class servletConfigDemo extends HttpServlet{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletConfig config=getServletConfig();
//1.getInitParameter(name)方法
String success=config.getInitParameter("success");
String error=config.getInitParameter("error"); System.out.println("success-----"+success);
System.out.println("errror------"+error); //2.getInitParameterNames方法
Enumeration enumeration=config.getInitParameterNames();
while(enumeration.hasMoreElements()){
String name=(String)enumeration.nextElement();
String value=config.getInitParameter(name);
System.out.println("name-----"+name);
System.out.println("value-----"+value);
} //3getServletContext方法
ServletContext servletContext=config.getServletContext();
System.out.println("servletContext----"+servletContext); //4.getServletName方法
String servletName=config.getServletName();
System.out.println("servletName------"+servletName); request.setCharacterEncoding("UTF-8");
String userId=request.getParameter("userId");
String passwd=request.getParameter("passwd"); //判断
if(userId!=null&&"gys".equals(userId)&&passwd!=null&&"gys".equals(passwd)){
HttpSession session=request.getSession();
session.setAttribute("user", userId);
//跳转
RequestDispatcher requestDispatcher=request.getRequestDispatcher(success);
requestDispatcher.forward(request, response);
}
else {
//跳转
RequestDispatcher requestDispatcher=request.getRequestDispatcher(error);
requestDispatcher.forward(request, response);
} }
}
index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>登陆</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="servletConfigDemo.do" method="post">
用户名<input type="text" value="" name="userId" /><br/>
密码:<input type="password" name="passwd" />
<input type="submit" value="提交" />
</form> </body>
</html>
实现登陆功能了
servlet 3.0无需配置web.xml,使用注入方式配置servlet实现登陆功能(服务器需要支持servlet3.0)的更多相关文章
- WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用
本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...
- Servlet的运行流程以及web.xml文件中的几种配置方式
Servlet的运行流程: 有俩种方式: 1.创建一个包,然后在包下创建一个class,class里面需要继承httpservlet,复写service方法------>配置web.xml文件: ...
- SpringMVC4零配置--web.xml
servlet3.0+规范后,允许servlet,filter,listener不必声明在web.xml中,而是以硬编码的方式存在,实现容器的零配置. ServletContainerInitiali ...
- web.xml中servlet-mapping的配置
<servlet-mapping>元素在Servlet和URL样式之间定义一个映射.它包含了两个子元素<servlet- name>和<url-pattern>,& ...
- SpringMVC(十四):SpringMVC 与表单提交(post/put/delete的用法);form属性设置encrypt='mutilpart/form-data'时,如何正确配置web.xml才能以put方式提交表单
SpringMVC 与表单提交(post/put/delete的用法) 为了迎合Restful风格,提供的接口可能会包含:put.delete提交方式.在springmvc中实现表单以put.dele ...
- Servlet(九):web.xml文件和server.xml文件
Web.xml 文件使用总结:作用: 存储项目相关的配置信息,保护 Servlet.解耦一些数据对程序的依赖.使用位置: 每个 Web 项目中Tomcat 服务器中(在服务器目录 conf 目录中)区 ...
- SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】
配置web.xml(struts2的过滤器.spring监听器.解决Hibernate延迟加载问题的过滤器.解决中文乱码的过滤器) <!-- 解决中文乱码问题 --> <filter ...
- 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中
把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...
- 创建新的servlet一定要记得修改web..xml文件!!!
创建新的servlet一定要记得修改web..xml文件!!!
随机推荐
- Message: dlopen failed for module ‘x’: file not found
这是未安装bochs-x的缘故 解决方案: sudo apt-get install bochs以后接着安装bochs-x. sudo apt-get install bochs-x 2.bx_dbg ...
- http cookie的domain使用
问题描述 最近遇到了一个因cookie domain设置不正确导致公司自研的分布式session组件无法生效的问题. 公司自研的这套分布式session组件依赖于设置在cookie中的sessionI ...
- Kolakoski数列
2018-04-16 15:40:16 Kolakoski序列是一个仅由1和2组成的无限数列,是一种通过“自描述”来定义的数列.他在整数数列大全网站上排名第二位,足见该数列在组合数学界中的重要性. K ...
- UVA-820 Internet Bandwidth (最大流)
题目大意:单源单汇无向网络求最大流. 题目分析:入门级别的题.但是ISAP在这儿好像不大好使?... 代码如下: # include<iostream> # include<cstd ...
- echart3 湖北地图及如何创建其他省份地图
刚刚收到一封园友求助echart湖北地图的邮件,现在将湖北地图的所有代码贴到这里,希望可以帮助到更多朋友. 1.首先你得到echarts官网下载js,很多人说找不到,可以到我的git下载(https: ...
- Kubernetes实践--hello world 示例
本文所说的Hello world是一个web留言板应用,并且是基于PHP+Redis的两层分布式架构的web应用,前端PHP web网站通过访问后端Redis数据库完成用户留言的查询和添加功能,具备读 ...
- BZOJ2620 [Usaco2012 Mar]Haybale Restacking
恩,非常好的题...至少思路非常巧妙 首先可以得到性质:对于相邻的两堆A & B,A给B然后B再给A是完全没有意义的...也就是说只能单向传递 然后我们记下每个点要给(被给)多少堆干草a[i] ...
- ISE创建Microblaze软核(二)
ISE创建Microblaze软核(二) (2012-07-13 15:09:08) 转载▼ 标签: 杂谈 分类: FPGA开发 第四步 进入Platform Studio操作界面 通过向导创建软核后 ...
- Robot Framework中使用HttpLibrary教程and中文支持
Robot Framework中使用and转参数时,默认不支持中文模式,如图场景: 会出现这种错误 FAIL : UnicodeDecodeError: 'ascii' codec can't dec ...
- Redis之数据持久化RDB与AOF
Redis之数据持久化RDB与AOF https://www.cnblogs.com/zackku/p/10087701.html 大家都知道,Redis之所以性能好,读写快,是因为Redis是一个内 ...