首先申明上面的报错红叉,我也不知道怎么回事.总之能运行.

新建项目时选择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)的更多相关文章

  1. WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用

    本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...

  2. Servlet的运行流程以及web.xml文件中的几种配置方式

    Servlet的运行流程: 有俩种方式: 1.创建一个包,然后在包下创建一个class,class里面需要继承httpservlet,复写service方法------>配置web.xml文件: ...

  3. SpringMVC4零配置--web.xml

    servlet3.0+规范后,允许servlet,filter,listener不必声明在web.xml中,而是以硬编码的方式存在,实现容器的零配置. ServletContainerInitiali ...

  4. web.xml中servlet-mapping的配置

    <servlet-mapping>元素在Servlet和URL样式之间定义一个映射.它包含了两个子元素<servlet- name>和<url-pattern>,& ...

  5. SpringMVC(十四):SpringMVC 与表单提交(post/put/delete的用法);form属性设置encrypt='mutilpart/form-data'时,如何正确配置web.xml才能以put方式提交表单

    SpringMVC 与表单提交(post/put/delete的用法) 为了迎合Restful风格,提供的接口可能会包含:put.delete提交方式.在springmvc中实现表单以put.dele ...

  6. Servlet(九):web.xml文件和server.xml文件

    Web.xml 文件使用总结:作用: 存储项目相关的配置信息,保护 Servlet.解耦一些数据对程序的依赖.使用位置: 每个 Web 项目中Tomcat 服务器中(在服务器目录 conf 目录中)区 ...

  7. SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】

    配置web.xml(struts2的过滤器.spring监听器.解决Hibernate延迟加载问题的过滤器.解决中文乱码的过滤器) <!-- 解决中文乱码问题 --> <filter ...

  8. 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中

    把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...

  9. 创建新的servlet一定要记得修改web..xml文件!!!

    创建新的servlet一定要记得修改web..xml文件!!!

随机推荐

  1. python函数返回局部变量,局部&全局变量同名问题

    其实关于返回局部变量不只是python的问题,凡是使用堆栈结构处理函数的语言都会有这样的问题,切记不要返回局部变量.因为当创建函数的堆栈撤销,所有对局部变量的修改都灰飞烟灭.来看我的小例子 def h ...

  2. hdu KiKi's K-Number 主席树

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. hihocode 股票价格 优先队列+map

    股票价格 时间限制:20000ms 单点时限:2000ms 内存限制:256MB 描述 小Hi最近在分析一支股票的价格走势,他需要一个程序来辅助分析.这个程序会接收3种消息(指令): 价格信息,格式是 ...

  4. [原][译][osgearth]关于Features & Symbology (特征与符号)(OE绘制矢量几何与特殊字符)讲解(OE官方文档翻译)

    原文参考:http://docs.osgearth.org/en/latest/user/features.html 自己翻译的,本人英文水平有限,有问题看原链接,原文 20170802重置修改部分翻 ...

  5. Idea使用(摘抄至java后端技术公众号-孤独烟)

    1. idea自动编译需要手动开启: 2. 手动去掉idea自动提示时候不区分字母大小写 3. idea自动导入包 4. 悬浮开关提示:鼠标放上去就给出提示 5. 打开的所有类tabs换行显示,不单行 ...

  6. laravel中数据库迁移的使用:

    创建数据库迁移文件: php artisan make:migration create_links_table 创建完表之后,设置字段: public function up() { Schema: ...

  7. C++设计模式之备忘录模式

    备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态[DP].举个简单的例子,我们玩游戏时都会保存进度,所保存的进度以文件的 ...

  8. SQL (FMDB)

    sql常用语句 创建表 CREATE TABLE IF NOT EXISTS "T_Person" ( "id" INTEGER NOT NULL PRIMAR ...

  9. Alpha冲刺一 (5/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9989898.html 作业博客:https://edu.cnblogs.com/campus/ ...

  10. 配置java环境变量,实现一条命令自由切java7 或java8

    在多个java编译环境中,有时需要java 7,有时又需要java 8,怎么配置java 环境,可以快速自动切换呢?下面用mac演示在 /etc/bashrc 中配置的环境变量 # 设置 JDK ex ...