Home > Back-end >  For the first time to write servlet question about the configuration
For the first time to write servlet question about the configuration

Time:05-11

In the web. The XML file to add the following code words all pages cannot directly from IDEA upper right to access, remove after can access,
 & lt; Servlet> 
HelloServlet
Com. Example. MyWebsite. HelloServlet


HelloServlet
/hello

Behind try many times, found that in fact is the mapping problem, to the point after the mapping can normal visit, excuse me what reason be, will be the servlet write
 package com. Example. MyWebsite; 

Import the Java. IO. *;
The import javax.mail. Servlet. ServletConfig;
import javax.servlet.ServletException;
The import javax.mail. Servlet. HTTP. *;
The import javax.mail. Servlet. The annotation. *;

@ WebServlet (name="helloServlet" value="https://bbs.csdn.net/hello-servlet")
Public class HelloServlet extends the HttpServlet {
private String message;

Public void init (ServletConfig config) throws ServletException {

}

Public void doGet (it request, HttpServletResponse response) throws IOException, ServletException {
String name=request. The getParameter (" name ");
response.setContentType("text/html; charset=utf-8");
PrintWriter out=response. GetWriter ();
Out.println (" & lt; Html>" +
"& lt; head> The Servlet running & lt;/title> </head>" + <br/>"& lt; body> <The h2 & gt; Hello "+ name +" & lt;/h2 & gt; </body>" + <br/>"& lt;/HTML "); <br/>} <br/>Public void doPost (it request, HttpServletResponse response) throws IOException, ServletException {<br/>DoGet (request, response); <br/>} <br/>Public void destroy () {<br/>} <br/>} </pre> <br/>The JSP code: <br/><pre> & lt; % -- -- <br/>Created by IntelliJ IDEA. <br/>User: luo xin '<br/>Date: 2021/5/9 <br/>Time: : <br/>To change this template use File | Settings | File Templates. <br/>- % & gt; <br/><% @ page contentType="text/HTML. Charset=utf-8 "language=" Java "% & gt; <br/><html> <br/><head> <br/><title> Welcome & lt;/title> <br/></head> <br/><body> <br/><The h2 & gt; Servlet</h2 & gt; <br/><The form method="post" action="/hello" & gt; <br/>Name: & lt; Input the name="name" type="text" & gt; <br/><The button type="submit" & gt; Submit & lt;/button> <br/></form> <br/></body> <br/></html> <br/></pre><p class="article - content rp"> CodePudding user response: </p>Why learn the ancient servlet <br/><br/>Springboot it is not sweet <br/><br/><br/>Shouldn't the servlet class init or service <br/><br/>You put the code in the init or service<p class="article - content rp"> CodePudding user response: </p>In the Java method wrote <br/><br/>@ WebServlet (name="helloServlet" value="https://bbs.csdn.net/hello-servlet") <br/>Don't have to web, XML to add <br/><Servlet> <br/><The servlet - name> HelloServlet</servlet - name> <br/><The servlet - class> Com. Example. MyWebsite. HelloServlet</servlet - class> <br/></servlet> <br/><The servlet - mapping> <br/><The servlet - name> HelloServlet</servlet - name> <br/><url-pattern>/hello</url - pattern> <br/></servlet - mapping><p class="article - content rp"> CodePudding user response: </p><fieldset> <legend> refer to the second floor afrgefitx response: <legend> <blockquote> Java method wrote <br/><br/>@ WebServlet (name="helloServlet" value="https://bbs.csdn.net/hello-servlet") <br/>Don't have to web, XML to add <br/><Servlet> <br/><The servlet - name> HelloServlet</servlet - name> <br/><The servlet - class> Com. Example. MyWebsite. HelloServlet</servlet - class> <br/></servlet> <br/><The servlet - mapping> <br/><The servlet - name> HelloServlet</servlet - name> <br/><url-pattern>/hello</url - pattern> <br/></servlet - mapping> </blockquote> <fieldset> I also just know, but not changed after ah ~ ~ ~ <br/><img SRC="https://img.codepudding.com/202105/194103110021231.png" Alt=""/> <br/>Don't know you the problem of path<p class="article - content rp"> CodePudding user response: </p>@ WebServlet (urlPatterns="/wxcallback", loadOnStartup=1) <br/>Or like this? <br/>Or <br/><br/>Web, XML <br/><Servlet> <br/><The servlet - name> DownloadServlet</servlet - name> <br/><The servlet - class> Beans. DownloadServlet</servlet - class> <br/></servlet> <br/><The servlet - mapping> <br/><The servlet - name> DownloadServlet</servlet - name> <br/><url-pattern> *. Do</url - pattern> <br/></servlet - mapping><p class="article - content rp"> CodePudding user response: </p>You stick the error code<p class="article - content rp"> CodePudding user response: </p><fieldset> <legend> reference 5 floor afrgefitx reply: <legend> <blockquote> you stick the error code out </blockquote> <fieldset> is can't find the page 404 </div> <div class="th_page th_page_color"></div> <div class="umCopyright"> <p>Page link:<a href="/Backend/110688.html" target="_blank" style="color:#999">https//www.codepudding.com/Backend/110688.html</a></p> </div> <div class="detail-arr"> <div class="detail-arr-left">Prev:<a href='/Backend/110687.html'>How to use Termux install Java</a></div> <div class="detail-arr-right">Next:<a href='/Backend/110689.html'>Java programmers what level can be 35 don't retire?</a></div> </div> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="hot-tags neitags"> <ul> <li><i class="iconfont icon-x-tags"></i> Tags:  </li> <a href='/e/tags/?tagname=Web+development' target='_blank'>Web development</a> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="xiangguan"> <ul class="msg msghead"> <li class="tbname">Related</li> </ul> <ul> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="flinks"> <ul> <li><i class="iconfont icon-x-tags"></i> Links:  </li> <li class="liflinks"><a target="_blank" href="/" title="CodePudding">CodePudding</a></li> </ul> </div> </div> </div> </div> <div class="footer"> <p><span style="font-size:16px;color:#666;font-weight: bold">About Us:</span>  <a href="https://www.codepudding.com/contact.html">Contact Us</a>      <a href="https://www.codepudding.com/service.html">Terms of Service</a>      <a href="https://www.codepudding.com/privacy.html"> Privacy Policy</a></p> <p class="foot_info">Copyright © 2010-2023,Powered By <a href="/" target="_blank">CodePudding</a> </p> </div> <script type="text/javascript" src="/skin/code/tianhu.js"></script> </body></html>