java web入门

package com.servlet;

import java.io.IOException;

import javax.jws.WebService;

import javax.servlet.Servlet;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebServlet;

/*@WebServlet("/demo1")*/
/*@WebServlet(urlPatterns = "/demo1",name = "demo1",
initParams={@WebInitParam(name="user",value="root"),
@webInitParam(name="pwd",value="root")},loadOnStaryup=1)*/

public class ServletDemo1 implements Servlet{

public void destroy() {

System.out.println("这是ServletDemo1的destroy销毁!");

}

public ServletConfig getServletConfig() {

System.out.println("这是ServletDemo1的getServletConfig配置!");

return null;

}

public String getServletInfo() {

System.out.println("这是ServletDemo1的getServletInfo信息!");

return null;

}

public void init(ServletConfig config) throws ServletException {

System.out.println("这是ServletDemo1的init初始化!");

String user = config.getInitParameter("user");

System.out.println(user);

String pwd = config.getInitParameter("pwd");

System.out.println(pwd);

}

public void service(ServletRequest arg0, ServletResponse arg1)

throws ServletException, IOException {

System.out.println("这是ServletDemo1的service服务!");

}

}

用户爱好注册

reg.jsp

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'reg.jsp' starting page</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表单中默认的提交方式为get -->

<form action="demo1" method="post">

用户名:<input type="text" name = "name"><br>

密码:<input type="password" name = "pwd"><br>

爱好:<input type="checkbox" name = "hobbys" value="shopping">shopping

<input type="checkbox" name = "hobbys" value="play">play

<input type="checkbox" name = "hobbys" value="读书">读书<br>

<input type="submit" value="注册"><input type = "reset" value="重置">

</form>

</body>

</html>

Demo1

package com.servlet;

import java.io.IOException;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

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("/demo1")

public class Demo1 extends HttpServlet{

/*protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

form表单中默认的提交方式为get get使用servlet中的编码格式
* 方法1、<form action="demo1" method="get">
* 统一设置编码格式 方法2、 <form action="demo1" method="post">
* 调用request.setCharacterEncoding("UTF-8")*/
//request.getParameter("name")获取form表单中name属性对应名称的value值

/*String name = request.getParameter("name");

System.out.println(name);

String pwd = request.getParameter("pwd");

System.out.println(pwd);

String sex = request.getParameter("sex");

System.out.println(sex);

String hobbys = request.getParameter("hobbys");

System.out.println(hobbys);

String[] hobbys = request.getParameterValues("hobbys");

for (String hobby : hobbys) {

System.out.println(hobby);

}

}*/

//get请求和post请求的区别 post请求地址栏更安全

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("get请求");

String name = request.getParameter("name");

System.out.println(name);

String pwd = request.getParameter("pwd");

System.out.println(pwd);

}@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("post请求");

String name = request.getParameter("name");

System.out.println(name);

String pwd = request.getParameter("pwd");

System.out.println(pwd);

//getParameterMap()获取所有的请求参数,并以Map集合方式返回

Map<String, String[]> map = request.getParameterMap();

Set<Entry<String, String[]>> set = map.entrySet();

for (Entry<String, String[]> entry : set) {

System.out.println(entry.getKey()+":"+entry.getValue());

}

}

}

相关推荐
cs_dn_Jie26 分钟前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
测开小菜鸟26 分钟前
使用python向钉钉群聊发送消息
java·python·钉钉
开心工作室_kaic1 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿1 小时前
webWorker基本用法
前端·javascript·vue.js
P.H. Infinity1 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天1 小时前
java的threadlocal为何内存泄漏
java
caridle2 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
cy玩具2 小时前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
^velpro^2 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋32 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx