简体   繁体   English

Java JSP getParameter显示空字

[英]Java JSP getParameter display null word

I have a "Hello World JSP" i need to get the users name from the URL and display it, It works fine but it needs to display a generic "World" if not user name is there. 我有一个“ Hello World JSP”,我需要从URL中获取用户名并显示它,它可以正常工作,但是如果没有用户名,则需要显示一个通用的“ World”。

Im not sure how to accomplish that? 我不确定如何做到这一点? were do i put "world" in my code to make it display if no user name is present? 如果没有用户名,我应该在代码中放入“世界”以使其显示吗?

Thanks! 谢谢!

 <HTML>
<BODY>
Hello <b><%= request.getParameter("user") %>World</b>!
</Body>
</HTML>

You can avoid the scriptlet by using JSP EL's ternary operator: 您可以通过使用JSP EL的三元运算符来避免scriptlet:

<HTML>
<BODY>
Hello <b>${not empty requestScope.user ?  requestScope.user: 'World'}</b>!
</Body>
</HTML>

Because you want to do this in an old fashioned way , do as below: 因为您想以老式的方式进行操作 ,所以请执行以下操作:

<%

String user = "", wordDisplay = "";

user = request.getParameter("user");

if(user != null && !user.equals(""))    
wordDisplay = user;     
else    
wordDisplay = "World";

%>

<HTML>
<BODY>
Hello <b><%= wordDisplay %></b>!
</Body>
</HTML>

I suggest you to do not use style of coding. 我建议您不要使用编码样式。 But still if you are interested ,then 但是如果你有兴趣的话

Try this :- 尝试这个 :-

<%
String vuser=request.getParameter("user");
if(vuser==null || vuser.equals("")){
   vuser="World";
}

%>

JSP Code :- JSP代码:-

<HTML>
   <BODY>
       Hello <%=vuser%> 
   </Body>
</HTML>

Hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM